Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push Notification Badges not Coming

I am using this coding for apple push notification, push notifications are coming but they are coming without any badges, any suggestion what is wrong with this code, that I am not getting badges. I already check the setting tab, and badges are on there.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

[[UIApplication sharedApplication] 
     registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeAlert | 
      UIRemoteNotificationTypeBadge | 
      UIRemoteNotificationTypeSound)];

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

}

- (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);
    //[self addMessageFromRemoteNotification:userInfo];

    NSString* alertValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"];
    NSLog(@"my message-- %@",alertValue);
    int badgeValue= [alertValue intValue];

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeValue];


}
like image 774
Syed Faraz Haider Zaidi Avatar asked Mar 16 '12 09:03

Syed Faraz Haider Zaidi


2 Answers

There was a problem with my server side coding as I just find out that badge value has to be implicitly set as integer to get desired result.

As I'm getting null value in badge value.

like image 107
Syed Faraz Haider Zaidi Avatar answered Nov 06 '22 02:11

Syed Faraz Haider Zaidi


I have faced this issue before , hope this helps

{"aps":{"alert":"dsfdsfsdfsdfsdfdfdfsdfsdf","badge":1,"sound":"a"}}

make sure there is no double quotation mark on badge value

like image 31
chings228 Avatar answered Nov 06 '22 02:11

chings228