推送通知到达时,应用程序徽章图标未更新

Max*_*Max 4 iphone notifications push-notification badge ios

我看过对问题的更新程序徽章在午夜选项:应用程序无法启动或背景,徽章数量可能减少 ,并更改应用程序图标徽章时通知到达推送通知徽章计数不更新更新徽章图标当应用程序被关闭,许多更多,但我有同样的问题,当应用程序在后台和推送通知到达时,应用程序徽章图标不更新.

我检查了所有可能性.我的代码是:

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

    UIApplicationState appState = UIApplicationStateActive;
    if ([application respondsToSelector:@selector(applicationState)]) {
        appState = application.applicationState;
    }

    for (id key in userInfo) {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }

    NSLog(@"remote notification: %@",[userInfo description]);

    [UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo valueForKey:@"aps"] valueForKey:@"badge"] integerValue];

    UIApplicationState state = [application applicationState];


    if (state == UIApplicationStateActive)
    {
        NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                            message:message
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
        [alertView show];

    }
 }
Run Code Online (Sandbox Code Playgroud)

是的,当app在前台时工作正常,然后它显示警告正常.但当应用程序处于后台时,既不会发生任何事件,也不会更新应用程序徽章图 我还设置了背景模式,如ios7.1所示:推送通知徽章更新问题,我还测试了应用程序徽章图标是否正确并在应用程序处于前台时打印在NSLog中.当应用程序在后台运行并且工作正常时,我需要将未读消息设置为徽章图标,但是当任何新推送通知到达时应该更新它.请建议我选择.

Max*_*Max 11

最后这个问题解决了.确实,当app处于后台时,没有一个动作被触发.所以当app在后台时也不会触发didReceiveRemoteNotification方法appDelegate.

当任何新推送通知到达设备时,应用程序徽章图标由iOS设置.有效载荷与@rahulPatel说的相同.我做了一点改变,因为BADGE_COUNT是从数据库中提取的,虽然它是INTEGER但是考虑到字符串和字符串不被iOS采用,这就是为什么我的应用程序的徽章图标在通知到达时不会更新.现在我从服务器端的代码变成这样:

$badges=(int)$cntmsg;

$body['aps'] = array(
                'alert' => $message,
                'sound' => 'default',
                'badge' => $badges
                //'msg_id'=> $msg_id
            );
Run Code Online (Sandbox Code Playgroud)

所以通过将计数msg更改为(int)它解决了我的问题.谢谢.