iphone:当应用程序处于非活动状态时,收到推送消息时在 tabbarItem 上设置徽章

发布于 2024-11-05 05:40:35 字数 1299 浏览 2 评论 0原文

我有一个使用 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 上都设置了徽章。

有人知道出了什么问题吗?

提前致谢!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征﹌骨岁月お 2024-11-12 05:40:35

如果应用程序处于非活动状态,则不会执行 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文