简体   繁体   English

增量后清除推送通知徽章

[英]Clear push notification badge after increment

I am working on push notification in iphone. 我正在开发iphone推送通知。 when i receive push notification, its showing 1 on my application icon, next time its 2,3,4. 当我收到推送通知时,它在我的应用程序图标上显示1,下次是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. 如果我打开应用程序的0.下一次它应该是1,2,3,4 ...但它显示最后一个数字和+1。 i want reset push notification badge after open application. 我打算在打开应用程序后重置推送通知徽章。 i am sending +1 from urban airship. 我正在从城市飞艇发送+1。

and its not working for me. 它不适合我。

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

I use this code in my app because the Urban Airship (UA) documentation said to 我在我的应用程序中使用此代码,因为Urban Airship(UA)文档说

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

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. 我在UA的论坛上看到了关于这个问题的一些帖子,他们没有给出明确的答案。

EDIT #1: 编辑#1:

I received the following response from a support technician at UA with the following suggestions, which worked great: 我收到了UA支持技术人员的以下回复,其中提出了以下建议:

What you want to do, is ensure that in your didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method, you are calling the following: 您要做的是确保在您的didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中,您调用以下内容:

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

and also call [[UAPush shared] resetBadge]; 并调用[[UAPush shared] resetBadge]; in the following methods as well: 在以下方法中:

applicationDidBecomeActive:(UIApplication *)application

and

didReceiveRemoteNotification:(NSDictionary *)userInfo

The technician also mentioned that assigning 0 to applicationIconBadgeNumber is not necessary, so I took it out. 技术人员还提到不需要为applicationIconBadgeNumber分配0,所以我把它拿出来了。 Still works beautifully. 仍然很漂亮。

EDIT #2: 编辑#2:

I ended up having to modify application:didReceiveRemoteNotification: to include a call to UA's handleNotification:applicationState: method: 我最终不得不修改application: didReceiveRemoteNotification:包含对UA的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. User is in app 用户在app中
  2. Push notification received 收到推送通知
  3. No badge was displayed on app icon when returning to home screen (as expected) 返回主屏幕时,应用程序图标上没有显示徽章(正如预期的那样)
  4. Another push notification is received 收到另一个推送通知
  5. Badge displayed number greater than 1 徽章显示的数字大于1

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. 我想你必须告诉UA,当收到通知并且应用程序在前台运行时,会处理通知。

I have used Urban Airship and have had this problem before. 我曾经使用Urban Airship并且之前遇到过这个问题。 You are not showing the code but i am assuming that when a notification is received, you are setting the application badge number to what urban airship is passing to you, don't do that. 您没有显示代码,但我假设在收到通知时,您将应用程序徽章编号设置为城市飞艇传递给您的,不要这样做。 Just let the application self handle this, when a remote notification comes in, let it auto increment on its own. 只需让应用程序自行处理,当有远程通知进入时,让它自动增加。 If that is not the case it could be that, on the urban airship side, you are setting a badge number to send with the push. 如果情况并非如此,那么在城市飞艇方面,您可以设置一个随推送发送的徽章编号。 Do not send a badge number with the push notification, leave that part out, iOS should auto increment your badge from its current badge number. 不要使用推送通知发送徽章编号,将该部分保留,iOS应自动从其当前徽章编号增加徽章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM