ios7.1: push notification badge update issue

2019-06-24 07:18发布

问题:

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

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.

回答1:

Try:

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


回答2:

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 
                 )
);