递增后清除推送通知标志

递增后清除推送通知标志

本文介绍了递增后清除推送通知标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究iPhone中的推送通知.当我收到推送通知时,它在我的应用程序图标上显示1,下次是2,3,4.如果我打开应用程序其0.下一次它应该是1,2,3,4 ...,但显示最后一个数字和+1.我想在打开应用程序后重置推送通知徽章.我正在从城市飞艇发送+1.

I am working on push notification in iphone. when i receive push notification, its showing 1 on my application icon, next time its 2,3,4. if i open application its 0. Next time its should be 1,2,3,4... but its showing last number and +1. i want reset push notification badge after open application. i am sending +1 from urban airship.

,它对我不起作用.

 [[UIApplication sharedApplication] cancelAllLocalNotifications];
 [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

推荐答案

我在我的应用中使用此代码,因为市区飞艇(UA)文档说

I use this code in my app because the Urban Airship (UA) documentation said to

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
[[UAPush shared] resetBadge];

但不起作用,应用程序图标上的徽章不断增加.我在UA的论坛上看到了一些有关此问题的帖子,但他们没有给出明确的答案.

but it doesn't work, the badge on the app icon keeps incrementing. I saw a few posts on this very issue on UA's forums and they haven't given a clear answer.

编辑#1:

我从UA的支持技术人员处收到以下答复,并提出了以下建议,这些建议非常有效:

I received the following response from a support technician at UA with the following suggestions, which worked great:

您要执行的操作是确保在您的 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中,您正在调用以下代码:

What you want to do, is ensure that in your didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method, you are calling the following:

[[UAPush shared] setAutobadgeEnabled:YES];
[[UAPush shared] resetBadge];//zero badge on startup

,也可以通过以下方法调用 [[UAPush shared] resetBadge]; :

and also call [[UAPush shared] resetBadge]; in the following methods as well:

applicationDidBecomeActive:(UIApplication *)application

didReceiveRemoteNotification:(NSDictionary *)userInfo

技术人员还提到不必为 applicationIconBadgeNumber 分配0,因此我将其取出.仍然效果不错.

The technician also mentioned that assigning 0 to applicationIconBadgeNumber is not necessary, so I took it out. Still works beautifully.

编辑#2:

我最终不得不修改 application:didReceiveRemoteNotification:以包括对UA的 handleNotification:applicationState:方法的调用:

I ended up having to modify application:didReceiveRemoteNotification: to include a call to UA's handleNotification:applicationState: method:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    // Get application state for iOS4.x+ devices, otherwise assume active
    UIApplicationState appState = UIApplicationStateActive;
    if ([application respondsToSelector:@selector(applicationState)])
    {
        appState = application.applicationState;
    }

    [[UAPush shared] handleNotification:userInfo applicationState:appState];
    [[UAPush shared] resetBadge];
}

因为我遇到以下情况的问题:

because I was having a problem with the following scenario:

  1. 用户在应用程序中
  2. 收到推送通知
  3. 返回主屏幕时,按预期,应用程序图标上没有显示徽章
  4. 收到另一个推送通知
  5. 徽章显示的数字大于1

通过上述修改,可以处理此情况.我想您必须告诉UA,当收到通知并且应用程序在前台运行时,通知已得到处理.

With the modification above, this scenario is handled. I guess you have to tell UA that the notification is handled when one is received and the app is running in the foreground.

这篇关于递增后清除推送通知标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:38