Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

ios - 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. 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. 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];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...