【问题标题】:iphone: Set badge on a tabbarItem when receiving a PUSH message, when the app is inactiveiphone:当应用程序处于非活动状态时,在收到 PUSH 消息时在 tabbarItem 上设置徽章
【发布时间】:2011-05-05 09:21:58
【问题描述】:

我有一个使用 PUSH 的应用程序。但是当应用程序处于非活动状态/在后台时,我遇到了一个问题。 当 PUSH 消息到来并且用户点击关闭时,徽章会设置在应用程序图标上。

但我也想在 tabBarItem 上设置一个徽章。 我有这段代码可以保存 PUSH

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  if (application.applicationState == UIApplicationStateInactive) {
    //Save the PUSH until the app is active.
    newPush = [userInfo copy];
  }
}

在:

- (void)applicationDidBecomeActive:(UIApplication *)application

我有以下代码:

//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];    
  }
}

我在应用程序处于活动状态时处理 PUSH 的代码工作正常,并且在应用程序图标和 tabBarItem 上都设置了徽章。

有人知道怎么了?

提前致谢!

【问题讨论】:

  • 我很确定除非应用程序处于活动状态,否则您不会收到 didReceiveRemoteNotification。或者在警报上单击打开时

标签: iphone apple-push-notifications


【解决方案1】:

如果应用程序处于非活动状态,则不会执行 didReceiveRemoteNotification。在这种情况下,通知数据可以到达您的应用程序的唯一方法是用户点击通知以打开应用程序。然后,当应用程序启动时,您可以使用以下代码获取application:didFinishLaunchingWithOptions: 中的通知数据:

- (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;
}

【讨论】:

    猜你喜欢
    • 2013-05-08
    • 2018-09-05
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多