简体   繁体   English

通知到达时更改应用程序图标标志

[英]Changing application icon badge when notification arrives

in my application I have registered for remote notification. 在我的应用程序中,我已经注册了远程通知。 when a user publishes some content, every user receives a notification. 当用户发布某些内容时,每个用户都会收到一条通知。 I am trying to count the number of notifications that arrives and update the icon badge number. 我正在尝试计算到达的通知数量并更新图标徽章编号。

If for example a user receives 3 notifications, the badge number must become 3, and when the user opens the app, the badge number goes back to 0. But I don't knw in which method to set the badge. 例如,如果某个用户收到3条通知,则徽章编号必须变为3,并且当用户打开应用程序时,徽章编号会回到0。但是我不知道使用哪种方法设置徽章。 I have tried this: 我已经试过了:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 2;    
// Handle Socialize notification at foreground
[self handleNotification:userInfo];
}

The problem with this is that the badge sets the number 2 when I actually open the app (bring it to foreground) if I have received notifications, and not when I receive the notification. 问题在于,如果我收到通知,则在我实际打开应用程序(将其带到前台)时,徽章将设置为2,而不是在收到通知时将其设置为2。 How can I solve this? 我该如何解决? Thanks for any help. 谢谢你的帮助。

EDIT: 编辑:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"1"] intValue];
// Handle Socialize notification at foreground
[self handleNotification:userInfo];
}

You need to determine the badge count on your server before you send the notification. 发送通知之前,您需要确定服务器上的徽章计数。 At the time the app received the notification it is too late to determine the correct number since the number from the notification immediately overwrites the number the app set. 应用收到通知时,确定正确的数字为时已晚,因为通知中的数字会立即覆盖应用设置的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM