How to increase the icon number when the application is in the background

I am using this code. Every thing works fine when a push notification appears, but the icon number does not increase when the application is in the background. How to solve this problem?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

    //UIApplication *application = [UIApplication sharedApplication];
    NSInteger badgeNumber = [application applicationIconBadgeNumber];// Take the current badge number
    //badgeNumber--;    // decrement by one
    [application setApplicationIconBadgeNumber:[[[launchOptions valueForKey:@"aps"]valueForKey:@"badge"]integerValue]];  // set ne badge number

    NSLog(@"userInfo :%@  %d",launchOptions,[[[launchOptions valueForKey:@"aps"]valueForKey:@"badge"]integerValue]);


    return YES;
}
+4
source share
3 answers

When the application is in the background, the method didFinishLaunchingWithOptionsnever calls. To do something when your application is in the background, you need to implement your logic in AppDelegate's applicationDidEnterBackground: a method like.

- (void)applicationDidEnterBackground:(UIApplication *)application{

[UIApplication sharedApplication].applicationIconBadgeNumber = 2;
}
+2
source

, . . , Push- . .

, push- 0. 0, 0 .

+2

When submitting Push Notification, you need to add an icon value. Thus, an icon icon is required for each user device. There is something like this in php:

$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge'=> 10
);

where 10 is just the number displayed in the icon.

+1
source

All Articles