Devendra Singh
Devendra Singh

Reputation: 777

How to increase badge number when application is in background

I am using this code. Every thing is working fine when push notification comes but badge number does not increase when application is in 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;
}

Upvotes: 5

Views: 13527

Answers (4)

OguzzYlcn
OguzzYlcn

Reputation: 69

You can increase badge number when your app is in background.

Write didReceiveRemoteNotification function to App Delegate and add 1 to application.applicationIconBadgeNumber

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 += 1 }

You can also reset badge number when app become active.

Write applicationDidBecomeActive function to App Delegate and make application.applicationIconBadgeNumber to 0

func applicationDidBecomeActive(_ application: UIApplication) {

application.applicationIconBadgeNumber = 0 }

Upvotes: 0

When you send a Push Notification you need add badge value. So you need handle badge for each user device. In php is something like that:

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

where 10 thats just the number displayed in the icon.

Upvotes: 1

user4034301
user4034301

Reputation:

You can't increase the badge number using code when app is in background or closed state.But the badge can be increased by value in notification payload.No code will execute during this state and Push notification is handled by OS itself. This link have the same issue and resolved.

Check whether the push notification payload contains application badge field and set to values greater than 0.If its 0,then badge number will be 0 when app in background or closed.

Upvotes: 2

Irfan
Irfan

Reputation: 4331

When the application is in the background didFinishLaunchingWithOptions method never calls. For doing something when your App is in background you need to implement your logic in AppDelegate's applicationDidEnterBackground: method like.

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

[UIApplication sharedApplication].applicationIconBadgeNumber = 2;
}

Upvotes: 2

Related Questions