Refresh push notification icon during application in background

I have a push notification and I was able to update the icon of the icon when the application is brought to the fore.

I am a little confused by this, though ... the iPhone receives a notification and a pop-up message appears to activate my application, and the icon only updates after the application starts.

This does not seem to be correct from the user's point of view. My understanding is that the icon counter should notify the user that it requires action by increasing the number, but this does not happen until a later stage when the application is active.

So, is there a way to tell the application to update the icon counter when it receives push notifications and while it is in the background?

Please note that my application does not use location and I have UIRemoteNotificationTypeBadge in the notification registration request.

Greetings AF

+56
ios apple-push-notifications
Jan 10 '13 at 11:15
source share
10 answers

Since the push notification is handled by iOS and not your application, you cannot change the application icon when receiving a push notification.

But you can send the icon number to the push notification payload, but you will have to run the calculation server command.

You should read Local and Push Notification Programming Guide and especially the Notification Payload .

The payload may look like this:

 { "aps" : { "alert" : "You got your emails.", "badge" : 9 } } 

Now the app icon icon will display 9.

+58
Jan 10 '13 at 11:25
source share

We can change the icon number when we are in the background by sending the icon icon to the push notification package. As @rckoenes noted, the JSON parameter for the icon must be INTEGER .

PHP code sample to do the same

 // Create the payload body $body['aps'] = array( 'alert' => $message, 'badge' => 1, 'sound' => 'default' ); 

badge => 1 where 1 is an integer, not a string (i.e., without an apostrophe)

+9
May 9 '13 at 7:26
source share
  **This is the APNS payload get back from server.** { "aps" : { "alert" : "You got your emails.", "badge" : 9, "sound" : "bingbong.aiff" }, "acme1" : "bar", "acme2" : 42 } 

The value for the key icon is automatically considered an icon counter. On the iOS side, there is no need to calculate or process the score. In example 9 above, the icon icon is located. Thus, your application icon will display 9.

NOTE. While your application is closed, you cannot handle the icons yourself. That's why we use the badge icon from APNS Payload. For a better explanation of the notification, see the Documentation.

if you want to reduce the number of icons on your own. Make a counter and update it yourself. Follow

+4
Apr 19 '17 at 14:16
source share

If you use NotificationServiceExtension, you can update the icon in this.

 var bestAttemptContent : UNMutableNotificationContent? // bestAttemptContent.badge = 0//any no you wanna display 

Each time your application receives a notification, an extension of your service will be called. Keep this user default and display it. To separate custom defaults between the application and the extension, you need to enable the application group in the application. More here

+2
Nov 08 '17 at 8:10
source share

With iOS 10, you can develop a notification service extension for your application. It will be launched by the system when you receive a notification, and you can calculate the actual number for the icon and set it.

Take a look at the documentation: https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension

+1
Nov 21 '17 at 8:30
source share
 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { application.applicationIconBadgeNumber = 0; NSLog(@"userInfo %@",userInfo); for (id key in userInfo) { NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]); } [application setApplicationIconBadgeNumber:[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]]; NSLog(@"Badge %d",[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]); } 
0
Jan 10 '13 at
source share

As @rckoenes said, you would have to do a server-side computing server, but how could you find out when you need to increase the value of the icon number that you need to send to the payload?

When the application starts, send a message to the server with a message about the application launch. Thus, on the server side, you start again with badge = 0, and as long as there are no messages received by the server, increase the icon number with each push notification payload.

0
Feb 16 '16 at 8:46
source share

In fact, in iOS 10, the remote notification will automatically didReceiveRemoteNotification method in the AppDelegate application.

You have 2 ways to refresh the icon in the background.
I have done this for my current application. You do not need to expand the eithier notification service.

1st way

Send the APS badge key with your payload to APN.
This will update the icon counter according to your Integer value in your icon payload. eg:

 // 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 } } 

2nd way

You can switch application application.applicationState and update your icons. When ApplicationState is in .background . BUT you should take care not to set the icon key parameter in your notification payload when sending to APN ex

 // Payload to APN as silent push notification { "aps": { "content-available": 1 } } 

Process update icon according to application state

Here is my working code for updating the badge counter without an icon in the APN payload.

 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") } } 

Reset number of icons

Remember to reset the icon counter when your application returns to its active state.

 func applicationDidBecomeActive(_ application: UIApplication) { // reset badge count application.applicationIconBadgeNumber = 0 } 
0
Nov 05 '18 at 17:58
source share

in the apns payload, you must define "available content": 1 to update the icon in the background

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // increase badge count, but no need if you include content-available application.applicationIconBadgeNumber = application.applicationIconBadgeNumber + 1 } func applicationDidBecomeActive(_ application: UIApplication) { // reset badge count application.applicationIconBadgeNumber = 0 } 

eg.

 "aps":{ "alert":"Test", "sound":"default", "content-available":1 } 
0
Dec 01 '18 at 9:49
source share

After receiving remote notification when opening the application

get the current icon number in the didBecomeActive <method. didBecomeActive .

File using the code below:

 int badgeCount = [UIApplication sharedApplication].applicationIconBadgeNumber; badgeCount = badgeCount + 1; 
-one
Nov 18 '14 at 13:32
source share



All Articles