Change the icon of the application icon when a notification arrives

In my application, I registered for remote notification. when a user publishes some content, each user receives a notification. I am trying to count the number of notifications that come and update the badge icon number.

If, for example, the user receives 3 notifications, the icon number should be 3, and when the user opens the application, the icon number returns to 0. But I do not know in which method to set the icon. I tried this:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 2;    
// Handle Socialize notification at foreground
[self handleNotification:userInfo];
}

The problem is that the icon sets number 2 when I actually open the application (bringing it to the forefront), if I received notifications, and not when I get a notification. How can i solve this? Thanks for any help.

EDIT:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"1"] intValue];
// Handle Socialize notification at foreground
[self handleNotification:userInfo];
}
0
1

. , , , , .

+3

Source: https://habr.com/ru/post/1542460/


All Articles