Push notification applicationIconBadgeNumber is not update in ios7

半城伤御伤魂 提交于 2019-12-24 03:47:07

问题


I am using following code in app delegate to receive Remote notification but its applicationIconBadgeNumber (appear on app icon's top left corner in red/white) is not updating when app in background. when push notification received its appear in top corner of screen with slide animation , in notification payload Badge count is comming form server side perfectly.

In didFinishLaunchingWithOptions I have put following code

[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

Delegates for Remote notification :

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *deviceTokenTrimmed = [[[[deviceToken description]
                                      stringByReplacingOccurrencesOfString:@"<"withString:@""]
                                     stringByReplacingOccurrencesOfString:@">" withString:@""]
                                    stringByReplacingOccurrencesOfString: @" " withString: @""];
    currentdeviceToken=deviceTokenTrimmed;
    [[NSUserDefaults standardUserDefaults] setValue:deviceTokenTrimmed forKey:@"pushtoken"];
    NSLog(@"Device Token didRegisterForRemoteNotificationsWithDeviceToken :  %@",deviceTokenTrimmed);

}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Device Token in FailToRegister RemoteNotifications ERROR %@",error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@"%@",userInfo);
    if (application.applicationState == UIApplicationStateActive)
    {
        /***********code to show alert********/
        if (![[[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]] isEqualToString:@""] && [[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]]!=nil) {
            NSString *MSG =[[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:MSG delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
            [alert show];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"Notification Received." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
            [alert show];
        }
    }else{
      application.applicationIconBadgeNumber =[[[userInfo objectForKey:@"aps"] objectForKey: @"badge"]integerValue];
    }

}

Thanks in advance.


回答1:


I found sol, Actually application:didReceiveRemoteNotification Not called when application in background, Its os responsibility to update applicationIconBadgeNumber when app in background, in my case actual problem was in payload. From Server End it was set as string but it should be integer. i am posting payload correct code which is written in php.

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'sound.caf',
'badge' => $badge_count // Should be int type
);

Thanks, mokujin and Himanshu




回答2:


Use methods of UIApplicationDelegate

As application:didReceiveRemoteNotification: method runs only when the application is in active state, the system calls this method regardless of the state of your app.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

       NSInteger badgeNumber = [application applicationIconBadgeNumber];
       [application setApplicationIconBadgeNumber:++badgeNumber];

    }



回答3:


Whith the JSON payload you're actually setting the badge number. The only solution is to manage it server side by notifying the server each time a "notification" is read.

I gave a logic that how you can implement search in google there are so many article relevant to it.



来源:https://stackoverflow.com/questions/22248149/push-notification-applicationiconbadgenumber-is-not-update-in-ios7

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