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
(setbadgeNumber
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
Swift - Using Cgcontext to Draw with Finger
How to Center a Label Horizontally for All iOS Devices in Swift
How to Create a CSV File from Core Data (Swift)
Automatically Change Cell Height Based on Content - Swift
Getting Uiwebview API Deprecation Message After Adding Paypal Pod in My iOS App
Core Data Entity Unique Constraint Does Not Work
Firebase Dynamic Link Doesnt Work in Safari Swift
How to Delete an Image from Photolibrary After I Pick It Up Using Uiimagepickercontroller
Swiftui - Optional Timer, Reset and Recreate
Most Efficient/Realtime Way to Get Pixel Values from iOS Camera Feed in Swift
Different Cell in Tableview Swift 3
How to Enable Only Numeric Digit Keyboard iOS Swift
How to Set Clear Background in Table View Cell Swipe Action
Saving Already Created Live Photos
How to Set Uitextview's Height Dynamically in Uitableviewcell Based on String Size