6

I have noticed that when a local notification is being received in an ios device, the notification appears in the Notification Center but the app badge number is not updated when the app is closed.

I need to touch the notification in the Notification Center for the local push message to be transferred to the app.

Is this the normal behavior? Can this be solved by using remote push notifications?

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Ketan
  • 487
  • 10
  • 23
  • 2
    Did you app in closed or in background? and It is also app number indication depended... – Mani Dec 26 '13 at 07:09
  • what do you mean "not updated"? the app badge nember didn't show the right number whit your localNotification.applicationIconBadgeNumber ? – Joiningss Dec 26 '13 at 07:16
  • @Mani The app is closed (not in background). `It is also app number indication depended`: Sorry, I don't understand what you mean here. – Ketan Dec 26 '13 at 10:32

2 Answers2

14

You can utilize the applicationIconBadgeNumber parameter in a UILocalNotification object.

Basically:

localNotificationObject.applicationIconBadgeNumber++;

Example:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:20];
localNotification.alertBody = @"Some Alert";

//the following line is important to set badge number
localNotification.applicationIconBadgeNumber++;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

But the issue with this is that the badge number doesn't increment on subsequent (multiple) local notifications (there's a scenario here but for simplicity sake, lets just say the badge stays 1 even after 2 or more, back to back, local notifications).
In this case, Yes... Push Notification seems to be the way to go
(but be aware that Push Notifications aren't always reliable... check: link)

Well... to use Push Notifications for proper badge number updates, you should know that you can send a badge count in the Push Notification's payload.
When this push notification is received, the badge count is changed by iOS to the badge count specified in the Push Notification (& the app need not be open for this).


Example (continued):

Set applicationIconBadgeNumber to 0 as it helps in certain scenarios (optional)

- (void)applicationWillResignActive:(UIApplication *)application {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

Extra:

You can also manually set the badge number when you terminate/close or resign the application.
Generally... in any or all of the following methods:

  • -applicationWillResignActive
  • -applicationDidEnterBackground
  • -applicationWillTerminate (set badgeNumber when app closes)

Example:

- (void)applicationWillResignActive:(UIApplication *)application {
    //Called when the application is about to move from active to inactive state.
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[UIApplication sharedApplication] scheduledLocalNotifications] count]];
    //...
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate.
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[UIApplication sharedApplication] scheduledLocalNotifications] count]];
    //...
}
Community
  • 1
  • 1
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
  • Very comprehensive answer. If I implement the code on terminating the app, my problem is not solved. Say I close my app today at 10am. I have a local push due at 2pm and one at 6pm. This means when I close my app, my badge number will be 2 at 10pm. What I actually want is to have badge count 1 at 2pm and 2 at 6pm. – Ketan Dec 26 '13 at 10:27
  • @Ketan : this is about `UILocalNotification` and not push notifications... right? – staticVoidMan Dec 26 '13 at 10:59
  • @Ketan : so, basically you want the local notification badge to appear/increment wen it occurs. so... if you set a local notification that will occur 30mins from now, then... you want the badge to appear/change only after 30mins? in this case, push notification seems to be the only way to go about it. check out this question: [link](http://stackoverflow.com/questions/5962054/iphone-incrementing-the-application-badge-through-a-local-notification) – staticVoidMan Dec 26 '13 at 11:38
  • @Ketan : sorry, i was wrong. i parted some wrong information. i've fixed my answer. now... the badge gets set on the local notification's `fireDate` (_even when app is closed_) – staticVoidMan Dec 27 '13 at 12:23
0

iPhone: Incrementing the application badge through a local notification

It is not possible to update dynamically the badge number with local notifications while your app is in the background. so You have to use push notifications. You can only increment badge while application is running in foreground and look for alternative solution you can go with here

iPhone: Incrementing the application badge through a local notification

Community
  • 1
  • 1
Retro
  • 3,985
  • 2
  • 17
  • 41