xcode 重置 iOS 应用徽章

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24219480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:05:22  来源:igfitidea点击:

Reset iOS app badge

iosxcodeios7parse-platformpush-notification

提问by Johan Enstam

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. My question is not how to reset the badge when i open the application because i already got that working with this code.

嗨,我目前正在开发一个使用推送通知的应用程序。我已经成功地让它与 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];
}

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。我该如何解决这个问题?

回答by Nitin Gohel

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;Not helps to clear badge in parse. I just read the Parse Push notification Guide Documentationand the Documentation said.

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;无助于清除解析中的徽章。我刚刚阅读了解析推送通知指南文档和文档说。

badge:The current value of the icon badge for iOS apps. Changing this value on the PFInstallation will update the badge value on the app icon. 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. This can be set to a value or to Increment in order to increment the current value by 1.

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

徽章:(仅限 iOS)应用程序图标右上角指示的值。这可以设置为一个值或 Increment,以便将当前值增加 1。

Clearing the BadgeYou need to do Code like:

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

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

回答by dstefanis

For anyone looking for how to reset the badge in swift, here's the swift version @nitin's answer which was spot on.

对于正在寻找如何快速重置徽章的任何人,这里是 swift 版本@nitin 的答案,这是正确的。

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