我看过关于的问题在午夜更新应用程序徽章,并提供以下选项:应用程序未启动或在后台,徽章数量可能会减少 https://stackoverflow.com/questions/4846787/updating-application-badge-at-midnight-with-options-app-not-launched-or-in-back
and 收到通知时更改应用程序图标徽章 https://stackoverflow.com/questions/14910871/changing-application-icon-badge-when-notification-arrives
and 推送通知徽章计数未更新 https://stackoverflow.com/questions/9742558/push-notification-badge-count-not-updating
and 应用程序关闭时更新徽章图标 https://stackoverflow.com/questions/17781130/update-badge-icon-when-app-is-closed还有更多,但我有同样的问题,当应用程序在后台并且推送通知到达时,应用程序徽章图标未更新。
我已经检查了所有的可能性。我的代码是:
-(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:推送通知徽章更新问题 https://stackoverflow.com/questions/22690160/ios7-1-push-notification-badge-update-issue我还测试了当应用程序位于前台时,应用程序徽章图标可以正确接收并打印在 NSLog 中。当应用程序进入后台且工作正常时,我需要将其未读消息设置为徽章图标,但当任何新的推送通知到达时,应该更新它。请建议我选择。
最后这个问题解决了。确实,当应用程序处于后台时,不会触发任何操作。所以didReceiveRemoteNotification
中的方法appDelegate
当应用程序处于后台时也不会触发。
当任何新的推送通知到达设备时,iOS 将设置应用程序徽章图标。有效负载与@rahulPatel 所说的相同。我对此做了很少的改变,因为 BADGE_COUNT 是从数据库中获取的,尽管它是 INTEGER,但考虑到字符串和字符串未被 iOS 获取,这就是为什么我的应用程序的徽章图标在通知到达时没有更新。现在我的服务器端代码变成这样:
$badges=(int)$cntmsg;
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => $badges
//'msg_id'=> $msg_id
);
所以通过将 count msg 更改为 (int) 它解决了我的问题。谢谢。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)