简体   繁体   English

重置iOS应用徽章

[英]Reset iOS app badge

Hi I'm currently developing an app which uses push notification. 嗨,我目前正在开发使用推送通知的应用程序。 I have successfully got it to work with Parse and my application is receiving the notifications. 我已经成功将其与Parse配合使用,并且我的应用程序正在接收通知。 My question is not how to reset the badge when i open the application because i already got that working with this code. 我的问题不是打开应用程序时如何重置徽章,因为我已经使用此代码进行操作了。

- (void)applicationDidBecomeActive:(UIApplication *)application
{

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

}

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// Store the deviceToken in the current installation and save it to   Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
}

That code removes the badge from the application but when i send another notification the number is now 2 instead of 1. How can i fix this? 该代码从应用程序中删除了徽章,但是当我发送另一个通知时,该号码现在是2而不是1。我该如何解决?

[UIApplication sharedApplication].applicationIconBadgeNumber = 0; Not helps to clear badge in parse. 无助于清除分析中的标志。 I just read the Parse Push notification Guide Documentation and the Documentation said. 我刚刚阅读了Parse Push通知指南文档,并说了“文档”。

badge: The current value of the icon badge for iOS apps. badge: iOS应用程序图标徽章的当前值。 Changing this value on the PFInstallation will update the badge value on the app icon. 在PFInstallation上更改此值将更新应用程序图标上的标志值。 Changes should be saved to the server so that they will be used for future badge-increment push notifications. 更改应保存到服务器,以便将来用于徽章增加推送通知。

badge: (iOS only) the value indicated in the top right corner of the app icon. 徽章:(仅限iOS)应用图标右上角指示的值。 This can be set to a value or to Increment in order to increment the current value by 1. 可以将其设置为一个值或“增量”,以使当前值增加1。

Clearing the Badge You need to do Code like: 清除徽章您需要执行以下代码:

- (void)applicationDidBecomeActive:(UIApplication *)application {
  PFInstallation *currentInstallation = [PFInstallation currentInstallation];
  if (currentInstallation.badge != 0) {
    currentInstallation.badge = 0;
    [currentInstallation saveEventually];
  }
  // ...
}

For anyone looking for how to reset the badge in swift, here's the swift version @nitin's answer which was spot on. 对于任何想快速重置徽章的人,这里都有@nitin的快速版本。

func applicationDidBecomeActive(application: UIApplication) {
    var current: PFInstallation = PFInstallation.currentInstallation()
    if (current.badge != 0) {
        current.badge = 0
        current.saveEventually()
    }
}

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

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