iPhone: How to set the application badge, when the app is closed/in background?
is it possible to set the applica开发者_Python百科tion badge, while the app is in background mode or even closed? I haven't found anything, yet.
From within your application you can use the applicationIconBadgeNumber
property of UIApplication
to set the badge number:
[UIApplication sharedApplication].applicationIconBadgeNumber = 1;
If you want to change the badge without the user launching your app, you need to use the push notification service. The Push Notification Service Programming Guide should have all the info you need.
It is possible via push notifications (as you've tagged) but bare in mind that if the user has disallowed push notifications it won't work.
To update the badge number via push, you need to include the following in your payload:
"badge" : 23
For more info about the payload, see this apple doc.
I was having difficulties with my app badge number which was being set as the accepted answer states.
I found using this subtly changed code worked better:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
Might be worth a check if you are having issues with your badge number
If you don't want to use push notification you can schedule a local notification to do this. This won't work if you force quit the application though.
精彩评论