ios7.1:推送通知徽章更新问题

我已经在我当前的项目中设置了Push Notification 。 我已经按照推送通知所需的所有指令。 在[标签:ios7],但在7.1我工作得很好,当我的应用程序在后台模式时,徽章更新问题。

我的代码如下:

 - (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没有得到调用,所以我做了一些search,并把:

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

并设置背景模式像: –

在这里输入图像说明

所有工作正常,当我的设备连接到Xcode和我运行应用程序testing推送通知但是当我拔掉设备(iOS7.1)。 然后推送通知抵达,但徽章不更新。 否则,在后台更新徽章以及调用所有方法。 但同样的事情,我testing这个应用程序到我的另一台iOS7设备工作得很好。

我不明白我的错误在哪里,我在代码中做错了什么地方。 有没有任何错误,或者我不这么认为,请帮我解决这个问题。

尝试:

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

最近我有这个问题,我不得不做一些演员:

 NSDictionary *apsData = [userInfo objectForKey:@"aps"]; NSInteger badgeNumber = (NSInteger)[[apsData objectForKey: @"badge"] intValue]; [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + badgeNumber; 

我正在使用PHP发送数据到苹果服务器,所以我也做了一个在那里:

 $playload = array('aps'=>array( 'message'=> 'some message', 'badge'=> (int)$badge ) );