5
votes

I have looked on question on Updating application badge at midnight with options: app not launched or in background, badge number can decrease and Changing application icon badge when notification arrives and Push Notification Badge Count Not Updating and Update badge icon when app is closed and many more but I have the same problem as when app is in background and push notification arrives, application badge icon is not updated.

I have checked all the possibilities for that. My code is :

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    UIApplicationState appState = UIApplicationStateActive;
    if ([application respondsToSelector:@selector(applicationState)]) {
        appState = application.applicationState;
    }

    for (id key in userInfo) {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }

    NSLog(@"remote notification: %@",[userInfo description]);

    [UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo valueForKey:@"aps"] valueForKey:@"badge"] integerValue];

    UIApplicationState state = [application applicationState];


    if (state == UIApplicationStateActive)
    {
        NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                            message:message
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
        [alertView show];

    }
 }

yes it is working fine when app is in foreground then it shows alert fine. but when app is in background, neither any event occurred nor application badge icon updated. I have also set the background mode as shown in ios7.1: push notification badge update issue and I also tested that application badge icon is received correct and printed in NSLog when app is in foreground. I need to set it unread messages as a badge icon when app is going in background and its working fine, but it should be updated when any new push notification arrives. Please suggest me option.

2
It must be manage from server side not from iOS side.iPatel
@iPatel yes, but why is the badge icon not updated when aps->badge is set? the badge icon should be set to the value of aps->badge when the notification arrives and the app cannot handle the notification because its in backgroundMichael
The server side code is ok and i have tested it. It gives perfect count result of badge count, i have checked by debugging, it gives result perfect when app is in foreground. But it should updated app icon badge when notification arrives and app is in background.Max

2 Answers

12
votes

Finally this issue is solved. It is true that no one action triggered when app is in background. so didReceiveRemoteNotification method in appDelegate also not triggered when app is in background.

The application badge icon is set by iOS when any new push notification arrives in the device. The payload is same as @rahulPatel says. I do little change in that as BADGE_COUNT is fetched from database, though it is INTEGER but consider as string and string is not taken by iOS thats why my app's badge icon is not updated when notification arrives. Now my code from server side becomes like this :

$badges=(int)$cntmsg;

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

so By changing count msg as (int) it solved my issue. Thanks.

3
votes

Usually in all apps, the unread notification counts are maintained in the server. When the server sends a push notification to a particular device token server sends the badge count along with the payload.

Your server logic needs to keep track of the proper badge count and send it appropriately.

{
    "aps" :  
    {
        "alert" : "Your notification message to show",
        "badge" : BADGE_COUNT ,
        "sound" : "sound.caf"
    }
}