推送通知到达时,应用程序徽章图标未更新 [英] application badge icon not updated when push notification arrives

查看:140
本文介绍了推送通知到达时,应用程序徽章图标未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在在午夜使用选项更新应用程序徽章:应用程序未启动或在后台,徽章编号可能会减少
在通知到达时更改应用程序图标徽章
推送通知徽章计数未更新
应用程序关闭时更新徽章图标等等,但我遇到与应用程序处于后台且推送通知到达时相同的问题,应用程序徽章图标未更新。

I have looked on question on Updating application badge at midnight with options: app not launched or in background, badge number can decrease and Changing application icon badge when notification arrives and Push Notification Badge Count Not Updating and Update badge icon when app is closed and many more but I have the same problem as when app is in background and push notification arrives, application badge icon is not updated.

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

I have checked all the possibilities for that. My code is :

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

    }
 }

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

yes it is working fine when app is in foreground then it shows alert fine. but when app is in background, neither any event occurred nor application badge icon updated. I have also set the background mode as shown in ios7.1: push notification badge update issue and I also tested that application badge icon is received correct and printed in NSLog when app is in foreground. I need to set it unread messages as a badge icon when app is going in background and its working fine, but it should be updated when any new push notification arrives. Please suggest me option.

推荐答案

最后这个问题解决了。确实,app在后台时没有触发任何动作。所以当应用程序处于后台时, appDelegate 中的 didReceiveRemoteNotification 方法也不会被触发。

Finally this issue is solved. It is true that no one action triggered when app is in background. so didReceiveRemoteNotification method in appDelegate also not triggered when app is in background.

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

The application badge icon is set by iOS when any new push notification arrives in the device. The payload is same as @rahulPatel says. I do little change in that as BADGE_COUNT is fetched from database, though it is INTEGER but consider as string and string is not taken by iOS thats why my app's badge icon is not updated when notification arrives. Now my code from server side becomes like this :

$badges=(int)$cntmsg;

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

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

so By changing count msg as (int) it solved my issue. Thanks.

这篇关于推送通知到达时,应用程序徽章图标未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆