推送通知applicationIconBadgeNumber未在ios7中更新

时间:2014-03-07 10:55:41

标签: ios iphone apple-push-notifications

我在app delegate中使用以下代码来接收远程通知,但是当app在后台时,它的applicationIconBadgeNumber(出现在应用程序图标左上角的红色/白色)不会更新。当推送通知收到它出现在幻灯片动画的屏幕上角时,在通知有效负载中,徽章计数完全从服务器端进入。

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];
    }

}

提前致谢。

3 个答案:

答案 0 :(得分:1)

我发现sol,实际应用程序:didReceiveRemoteNotification在后台应用程序时不调用,它负责在后台应用程序时更新applicationIconBadgeNumber, 在我的情况下,实际问题是有效载荷。从服务器端,它被设置为字符串,但它应该是整数。我发布有效负载正确的代码,用PHP编写。

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

谢谢,mokujin和Himanshu

答案 1 :(得分:0)

使用UIApplicationDelegate

的方法

由于application:didReceiveRemoteNotification:方法仅在应用程序处于活动状态时运行,因此无论应用程序的状态如何,系统都会调用方法。

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

       NSInteger badgeNumber = [application applicationIconBadgeNumber];
       [application setApplicationIconBadgeNumber:++badgeNumber];

    }

答案 2 :(得分:0)

在JSON有效负载上,您实际上正在设置徽章编号。唯一的解决方案是通过每次读取“通知”时通知服务器来管理服务器端。

我给出了一个逻辑,即如何在谷歌中实现搜索,有太多与之相关的文章。