本文介绍了ios7.1:推送通知徽章更新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我当前的项目之一中设置了推送通知.我已遵循推送通知所需的所有说明.在 [tag: ios7] 中工作正常,但在 7.1 中,当我的应用程序处于后台模式时,我的徽章更新出现问题.

I have setup Push Notification in One of my current Project. I have followed all instruction required for Push notification. that working fine in [tag: ios7] but in 7.1 i got issue in badge update when my application in background Mode.

我的代码如下:-

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

}


- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}


- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString *str = [NSString
                     stringWithFormat:@"%@",deviceToken];
    NSLog(@"%@",str);

    self.deviceToken = [NSString stringWithFormat:@"%@",str];
    NSLog(@"dev --- %@",self.deviceToken);
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@"<" withString:@""];
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@">" withString:@""];
    NSLog(@"dev --- %@",self.deviceToken);
}

为了得到回应

-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{

    UIApplicationState state = [application applicationState];

    // If your app is running
    if (state == UIApplicationStateActive)
    {

        //You need to customize your alert by yourself for this situation. For ex,
        NSString *cancelTitle = @"ok";
      //  NSString *showTitle = @"Get Photos";
        NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                            message:message
                                                           delegate:self
                                                  cancelButtonTitle:cancelTitle
                                                  otherButtonTitles:nil];
        [alertView show];


    }
    // If your app was in in active state
    else if (state == UIApplicationStateInactive)
    {

    }

     [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue];

}

第一件事是,当我的应用程序返回 didReceiveRemoteNotification 没有被调用时,我做了一些搜索并输入:-

First thing is that when my Application going to back ground that didReceiveRemoteNotification not getting called so i did some search and put:-

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

然后将背景模式设置为:-

And set Background Mode like:-

当我的设备与 xcode 连接并且我运行测试推送通知的应用程序时一切正常,但是当我拔下设备(iOS7.1)时.然后推送通知到达但徽章未更新.否则,该徽章会在后台更新以及调用的所有方法.但是同样的事情我在我的另一台设备上测试了这个应用程序,iOS7 一切正常.

That All working fine when my device is connect with xcode and i running the app testing Push Notification But when i unplugged device( iOS7.1). then push notification arrived but badge not update. Otherwise that badge update in background as well as all method called. But same thing i testing this app in to my another device with iOS7 that working all well.

我不明白我的错误在哪里以及我在代码中做错了什么.是否有任何错误或我不认为的内容,请帮助我解决这个问题.

I can't understand where is my mistake and where i did wrong in code. is there any bug or what i dont think so please help me out on this.

推荐答案

尝试:

int badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
[UIApplication sharedApplication].applicationIconBadgeNumber = 0; // try 0 or -1
[UIApplication sharedApplication].applicationIconBadgeNumber = badge + [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue];

这篇关于ios7.1:推送通知徽章更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 02:23