Set the icon on the application icon with push notifications when the application is in the background or the application was killed

Having completed almost all the lessons, I still cannot correctly set my icons. The situation is this: when my application is in the background, I want my icon to increase by 1. Thus, I use the bottom line of code

NSLog(@"In didReceiveRemoteNotification badge number is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber);
[UIApplication sharedApplication].applicationIconBadgeNumber += [[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] integerValue];
NSLog(@"In didReceiveRemoteNotification badge number is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber);

These two NSLogs give me the values ​​1 and 2 respectively. How before installing applicationIconBadgeNumber gets 1? And so it shows 2 as an icon when it receives one notification.

To make my question clearer I am sending all the code made to - didReceiveRemoteNotification

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler{

if ((application.applicationState ==  UIApplicationStateBackground)) {

    NSLog(@"===========================");
    NSLog(@"App was in BACKGROUND...");

    NSLog(@"In didReceiveRemoteNotification badge number is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber);
    [UIApplication sharedApplication].applicationIconBadgeNumber += [[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] integerValue];
    NSLog(@"In didReceiveRemoteNotification badge number is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber);
    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotif];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];}
if (application.applicationState ==  UIApplicationStateActive) {
    NSLog(@"===========================");
    NSLog(@"App was ACTIVE");

    NSString *str = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Notification Received" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    alertview.tag = 600;
    [alertview show];
}

NSLog(@"%@",userInfo);
handler(UIBackgroundFetchResultNewData);}

My second question: when my application is killed, I go for the code below: didFinishLaunchingWithOptions:

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
    [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + [[[localNotif valueForKey:@"aps"] valueForKey:@"badge"] integerValue];}

, Stackoverflow UIRemoteNotificationTypeNewsstandContentAvailability, . . 1.

.

+4
2

, iOS , aps → . , , , ().

iOS , . , n push-, , iOS n ( + n). , , , , .

, , , . badgeCount, , , push. , , reset, 0, 1.

+3

iOS, , .

.

, :

" push

0

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


All Articles