I am trying to update the badge icon for my app(closed) when I received a PN.
I have tried adding the codes into but it's not working when my app is closed. It works when the app is running in the foreground.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
//Accept push notification when app is not open
if (remoteNotif) {
[application setApplicationIconBadgeNumber:100];
return YES;
}
}
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 30];
}
If you app is closed or in the background, a Push notification won't wake it up. You need to do this server side and include the number you want to see on icon in your notification payload:
{
"aps" : {
"alert" : "Your notification message",
"badge" : 1
}
}
Have a look at the Apple doc on Push Notification programming guide
for that set applicationIconBadgeNumber = 1
or 0
in didFinishLaunchingWithOptions:
method like bellow...
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
See Another answer for UILocalNotification
From this link ios-badge-number-live-update
Also another link for RemoteNotifications from this link RemoteNotificationsPG Guide
Since push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification.
But you can send the badge number in the payload of the push notification, but the you will have to do the calculation server side.
The payload could look like this:
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 1
}
}
Now the app application badge icon will show 1.