Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

application badge icon not updated when push notification arrives

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.

like image 669
Max Avatar asked May 29 '14 10:05

Max


People also ask

Why are my notification badges not showing?

Turn on App icon badges from Settings. Navigate back to the main Settings screen, tap Notifications, and then tap Advanced settings. Tap the switch next to App icon badges to turn them on.

Why is my badge app icon not working?

If badge icons are not showing up properly, try the following troubleshooting tips. Delete any unnecessary apps and data on your device. Restart your device. Update the LINE app to the latest version here.

How do I get my app icon to show notification numbers?

If you want to change badge with number, you can be changed in NOTIFICATION SETTING on the notification panel or Settings > Notifications > App icon badges > Select Show with number. Skip to Step 4 if you use this method. Screen Images are for reference only.


2 Answers

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.

like image 126
Max Avatar answered Oct 24 '22 10:10

Max


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"
    }
}
like image 3
Rahul Patel Avatar answered Oct 24 '22 11:10

Rahul Patel