简体   繁体   中英

How to Increment the Push notification Badge iPhone

Is it possible to increment the badge value on receiving the notification. OR Should I send the count as payload ?

If i am sending the badge value as "1" every time, how could i increment the badge-value in the icon of the app if the app is not open.

I used this code but doesn't work.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1; 
}

If you want to change the icon badge automatically use the following code.

- (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]);

}

We also need to change the php file. So we can get the change the icon badge automatically

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

Since push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification.

But you can send the badge number in the payload of the push notification, but the you will have to do the calculation server side.

You should read :Local and Push Notification Programming Guide and specially the The Notification Payload.

The payload could look like this:

{ "aps" : { "alert" : "You got your emails.", "badge" : 9 } } Now the app application badge icon will show 9.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM