推送通知applicationIconBadgeNumber在ios7中不更新

我在应用程序委托中使用以下代码来接收远程通知,但是它的applicationIconBadgeNumber(在应用程序图标的左上angular以红色/白色出现)不会在后台应用程序中更新。 当收到的推送通知出现在幻灯片animation屏幕的上angular时,在通知载荷中徽章计数完美地从服务器端进行。

didFinishLaunchingWithOptions我把下面的代码

 [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 

代表远程通知:

 - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *deviceTokenTrimmed = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString: @" " withString: @""]; currentdeviceToken=deviceTokenTrimmed; [[NSUserDefaults standardUserDefaults] setValue:deviceTokenTrimmed forKey:@"pushtoken"]; NSLog(@"Device Token didRegisterForRemoteNotificationsWithDeviceToken : %@",deviceTokenTrimmed); } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"Device Token in FailToRegister RemoteNotifications ERROR %@",error); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"%@",userInfo); if (application.applicationState == UIApplicationStateActive) { /***********code to show alert********/ if (![[[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]] isEqualToString:@""] && [[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]]!=nil) { NSString *MSG =[[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]]; UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:MSG delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; [alert show]; }else{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"Notification Received." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; [alert show]; } }else{ application.applicationIconBadgeNumber =[[[userInfo objectForKey:@"aps"] objectForKey: @"badge"]integerValue]; } } 

提前致谢。

我发现溶胶,其实应用程序:didReceiveRemoteNotification当应用程序在后台不调用,它的操作系统责任更新applicationIconBadgeNumber当应用程序在后台,在我的情况下实际的问题是在有效载荷。 从服务器端它被设置为string,但它应该是整数。 我发布有效载荷正确的代码是用PHP编写的。

 // Create the payload body $body['aps'] = array( 'alert' => $message, 'sound' => 'sound.caf', 'badge' => $badge_count // Should be int type ); 

谢谢,mokujin和Himanshu

使用UIApplicationDelegate方法

作为application:didReceiveRemoteNotification:方法只在应用程序处于活动状态时运行,系统将调用方法,而不pipe应用程序的状态如何。

 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSInteger badgeNumber = [application applicationIconBadgeNumber]; [application setApplicationIconBadgeNumber:++badgeNumber]; } 

与JSON有效载荷,你实际上设置的徽章号码。 唯一的解决办法是每次读取“通知”时通知服务器来pipe理服务器端。

我给了一个逻辑,你怎么可以在谷歌实现search有这么多的文章有关。