application badge icon not updated when push notification arrives

狂风中的少年 提交于 2019-12-21 17:44:15

问题


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.


回答1:


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.




回答2:


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"
    }
}


来源:https://stackoverflow.com/questions/23931484/application-badge-icon-not-updated-when-push-notification-arrives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!