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.
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.