this is my push apple notification code when the app is running and a notification appears. I increase the score of the icon and get the desired result when I click the "Home" button on the application icon. but when I do not start my application and the notification comes, it did not increase the number of counter icons automatically and only 1 remains. The value 1 comes from the server. can anyone point out where i am doing wrong. thanks in advance.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
userMessageCounter = @"0";
postType = 0;
joinedStreamChecker = 0;
OwnerValue = 0;
pushValue = 1;
badgeValue =0;
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
{
pushValue = 0;
NSLog(@"notification off");
}
return YES;
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken1 {
NSString *str = [NSString
stringWithFormat:@"%@",deviceToken1];
NSLog(@"%@",str);
self.deviceToken = [NSString stringWithFormat:@"%@",str];
NSLog(@"dev --- %@",self.deviceToken);
self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@"<" withString:@""];
self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@">" withString:@""];
NSLog(@"dev --- %@",self.deviceToken);
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSString *str = [NSString stringWithFormat: @"Error: %@", err];
NSLog(@"%@",str);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Received notification: %@", userInfo);
NSString* alertValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"];
NSLog(@"my message-- %@",alertValue);
badgeValue= [alertValue intValue];
[UIApplication sharedApplication].applicationIconBadgeNumber += badgeValue;
}
source
share