Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios - local notification not updating badge number when application is closed

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?

like image 313
Ketan Avatar asked Dec 26 '13 07:12

Ketan


People also ask

Why are my app badges not showing numbers iPhone?

Check Settings app On your iPhone or iPad, leave Things and go to the Settings app. Tap Notifications. Scroll down to find Things and tap it. Enable the toggle for Badges (if it's already on, toggle it off and back on).

How do I get notification number for app icon on iPhone?

Step 1: Launch the Settings app on your iPhone or iPad. Step 2: Tap Home screen. Step 3: In the Notification badges section, toggle on the switch for Show in App Library.

How do you keep the icon badges on notification is cleared at the top of the screen?

If you go to Settings > Notifications > App Icon Badges > Number > Notifications on App Icons > On, this allows you press and hold on the app icon to view notifications. Also, when you swipe down your Notification Panel, swipe individual notifications away rather than hitting 'Clear' to prevent losing track of them.


1 Answers

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]];
    //...
}
like image 123
staticVoidMan Avatar answered Nov 14 '22 22:11

staticVoidMan