开发者

iphone: Set badge on a tabbarItem when receiving a PUSH message, when the app is inactive

I have a application that uses PUSH. But I have one problem when the application is inactive/in the background. When the PUSH messages come and the user clicks on Close, the badge is set on the application-icon.

But I also want to set a badge on a tabBarItem. I have this code that saves the PUSH

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  if (application.applicationState == UIApplicationStateInactive) {
    //Save the PUSH until the app is active.
    newPush = [userInfo c开发者_如何学Copy];
  }
}

And in:

- (void)applicationDidBecomeActive:(UIApplication *)application

I have the following code:

//Check if there is new PUSH messages.
if (newPush!=nil) {
  //There is a new PUSH!
  NSInteger badge = [[[newPush objectForKey:@"aps"] objectForKey:@"badge"] intValue];
  if (badge > 0) {
    //Set badge-numbers to 'badge'
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
    [[[[[self tabBarController] tabBar] items] objectAtIndex:3] setBadgeValue:[NSString stringWithFormat:@"%d",badge]];
  }
  else {
    //Set badge-numbers to zero
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    [[[[[self tabBarController] tabBar] items] objectAtIndex:3] setBadgeValue:nil];    
  }
}

My code for handling the PUSH when the application is active works fine and the badges are set both on the application-icon and on the tabBarItem.

Someone know what's wrong?

Thanks in advance!


If the application is inactive, didReceiveRemoteNotification is not executed. The only way the notification data can reach your app in this case is if the user taps on the notification to open the app. Then, when the app is launched, you can get the notification data in application:didFinishLaunchingWithOptions: by using this code :

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

    NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

    //Accept push notification when app is not open
    if (remoteNotif) {      
        [self handleRemoteNotification:application userInfo:remoteNotif];
        return YES;
    }

    return YES;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜