4
votes

I have setup Push Notification in One of my current Project. I have followed all instruction required for Push notification. that working fine in [tag: ios7] but in 7.1 i got issue in badge update when my application in background Mode.

My code is Following:-

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

}


- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}


- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString *str = [NSString
                     stringWithFormat:@"%@",deviceToken];
    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);
}

And for getting responce

-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{

    UIApplicationState state = [application applicationState];

    // If your app is running
    if (state == UIApplicationStateActive)
    {

        //You need to customize your alert by yourself for this situation. For ex,
        NSString *cancelTitle = @"ok";
      //  NSString *showTitle = @"Get Photos";
        NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                            message:message
                                                           delegate:self
                                                  cancelButtonTitle:cancelTitle
                                                  otherButtonTitles:nil];
        [alertView show];


    }
    // If your app was in in active state
    else if (state == UIApplicationStateInactive)
    {

    }

     [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue];

}

First thing is that when my Application going to back ground that didReceiveRemoteNotification not getting called so i did some search and put:-

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

And set Background Mode like:-

enter image description here

That All working fine when my device is connect with xcode and i running the app testing Push Notification But when i unplugged device( iOS7.1). then push notification arrived but badge not update. Otherwise that badge update in background as well as all method called. But same thing i testing this app in to my another device with iOS7 that working all well.

I can't understand where is my mistake and where i did wrong in code. is there any bug or what i dont think so please help me out on this.

2
Do your server send a badge in apn?cojoj
i am setting badge from my side as i mention in my above question.Nitin Gohel
But you are using remote notifications... It's server side which manages badges, not you on device.cojoj

2 Answers

1
votes

Try:

int badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
[UIApplication sharedApplication].applicationIconBadgeNumber = 0; // try 0 or -1
[UIApplication sharedApplication].applicationIconBadgeNumber = badge + [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue];
0
votes

I had this issue recently, I had to do some cast:

NSDictionary *apsData = [userInfo objectForKey:@"aps"];
NSInteger badgeNumber = (NSInteger)[[apsData  objectForKey: @"badge"] intValue];

[UIApplication sharedApplication].applicationIconBadgeNumber = 
            [UIApplication sharedApplication].applicationIconBadgeNumber + badgeNumber;

I was using PHP to send the data to apple server, so I also did a cast over there:

$playload = array('aps'=>array(
                         'message'=> 'some message',
                         'badge'=> (int)$badge 
                 )
);