Reputation: 251
I am sending notifications from server side to apns server,i was facing a small problem while handling badge,my badge number should be reduced if user has seen the notification in notification center, if app is already running no need of badge i think so, but always my badge number is "1"
from my server side i am sending badge="1".how to inform to server my app has particular badge number, how to know my badge number.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%@",userInfo);
UIApplicationState state = [application applicationState];
[[UIApplication sharedApplication]setApplicationIconBadgeNumber:1];
if (state == UIApplicationStateActive) {
[[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
message:message
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles: nil];
[alertView show];
[alertView release];
}
}
Upvotes: 1
Views: 1848
Reputation: 17419
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",
"badge" : badgecount ,
"sound" : "bingbong.aiff"
}
}
and instead in your code [[UIApplication sharedApplication]setApplicationIconBadgeNumber:1];
use
badge_value+=[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"]intValue];
[UIApplication sharedApplication].applicationIconBadgeNumber = badge_value;
where, badge_value is an integer that stores the badge value.
Upvotes: 1