Unusernotificationcenter.Current().Getdelivered
notifications Only Ever Returns an Empty Array

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

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]];
//...
}

Can UNUserNotificationCenter.getDeliveredNotifications() be used to retrieve user-directed push notifications

From the doc:

notifications

An array of UNNotification objects representing the
local and remote notifications of your app that have been delivered
and are still visible in Notification Center. If none of your app’s
notifications are visible in Notification Center, the array is empty.

So, yes it will return also push notifications not opened yet and visible in Notification Center.



Related Topics



Leave a reply



Submit