I have got push notification working and managed to update icon badge count when app is brought to foreground.
I am a bit confused about this though... the iPhone receives the notification and the pop up message appears to activate my app, and the badge only updates after me starting the app.
This does not sound right in terms of user experience. My understanding is that the badge count should notify the user of what needs action, through incremented count, but this does not happen until a later stage when the app is live.
So is there a way to tell the app to update its badge count when it receives push notifications and whilst being in the background?
Note that my app does not use location and that I have UIRemoteNotificationTypeBadge
in the notification registration request.
Open settings app > Apps > Tap on 3-dot menu in the top right corner > Special Access. Now tap on Notification access.
Check System Settings Go to System Settings → Notifications. Find Things in the list of apps. Toggle on Allow Notifications (if it's on, turn it off and back on). Toggle on Badge application icon (if it's on, toggle it off and on again).
Certain notifications do not lend themselves to the use of app icon badges, so you may want to disable the feature at these times. The feature makes little sense for notifications relating to time-sensitive alerts, like those of clocks and other alarms, for example.
Since push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification.
But you can send the badge number in the payload of the push notification, but the you will have to do the calculation server side.
You should read Local and Push Notification Programming Guide and especially the The Notification Payload.
The payload could look like this:
{ "aps" : { "alert" : "You got your emails.", "badge" : 9 } }
Now the app application badge icon will show 9.
Actually in iOS 10 a remote Notification will call automatically didReceiveRemoteNotification
Method in your AppDelegate.
You have 2 ways of updating the badge count in the background.
I've done this for my current app also. You don't need a Notification Service Extension either.
Send APS badge key with your payload to APN.
This will update the badge count according to your Integer value in your payload of badge. e.x.:
// Payload for remote Notification to APN { "aps": { "content-available": 1, "alert": "Hallo, this is a Test.", "badge": 2, // This is your Int which will appear as badge number, "sound": default } }
You can switch your application.applicationState and update your badges Count when the applicationState is in .background
. BUT you have to take care not to set the badge key parameter in your Notification payload when sending to APN e.x.
// Payload to APN as silent push notification { "aps": { "content-available": 1 } }
Here is my working code for badge count update without badge key in the payload for APN.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { print("APN recieved") // print(userInfo) let state = application.applicationState switch state { case .inactive: print("Inactive") case .background: print("Background") // update badge count here application.applicationIconBadgeNumber = application.applicationIconBadgeNumber + 1 case .active: print("Active") } }
Don't forget to reset your badge count when your app gets back to active state.
func applicationDidBecomeActive(_ application: UIApplication) { // reset badge count application.applicationIconBadgeNumber = 0 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With