ios - local notification not updating badge number

2019-04-28 13:48发布

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?

2条回答
神经病院院长
2楼-- · 2019-04-28 14:06

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

查看更多
对你真心纯属浪费
3楼-- · 2019-04-28 14: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]];
    //...
}
查看更多
登录 后发表回答