重置iOS应用徽章

时间:2014-06-14 11:31:54

标签: ios xcode ios7 parse-platform push-notification

您好,我目前正在开发一个使用推送通知的应用。我已经成功地使用它与Parse一起工作,我的应用程序正在接收通知。我的问题不是当我打开应用程序时如何重置徽章,因为我已经使用了这段代码。

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

该代码会从应用程序中删除徽章,但是当我发送另一个通知时,该号码现在为2而不是1.如何解决此问题?

2 个答案:

答案 0 :(得分:15)

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;无法清除解析徽章。我刚刚阅读了Parse Push notification Guide Documentation并且文档说了。

  

徽章:iOS应用的图标徽章的当前值。在PFInstallation上更改此值将更新应用程序图标上的徽章值。应将更改保存到服务器,以便将其用于将来的徽章增量推送通知。

     

徽章(仅限iOS)应用图标右上角显示的值。这可以设置为值或增量,以便将当前值增加1。

清除徽章您需要执行以下代码:

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

答案 1 :(得分:4)

对于那些正在寻找如何在swift中重置徽章的人来说,这是一个快速的版本@ nitin的回答。

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