ios 在 Swift 中更新徽章计数器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29843112/
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-08-31 05:45:28  来源:igfitidea点击:

Update badge counter in Swift

iosswiftparse-platformpush-notificationbadge

提问by Orkhan Alizade

With following code I get (2) in the badge icon immediately after app compiling:

使用以下代码,我在应用程序编译后立即在徽章图标中获得 (2):

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation.badge = 2
    installation.saveInBackground()
}

I did try the next variant: Initialized a new var badgeCount = 0and later:

我确实尝试了下一个变体:初始化一个新的var badgeCount = 0和后来的:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    badgeCount++
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation.badge = badgeCount
    installation.saveInBackground()
}

But when I get new notifications it doesn't update to +1. Is anyone know how to fix it?

但是当我收到新通知时,它不会更新为 +1。有谁知道如何解决它?

采纳答案by Swinny89

It won't update the badge number with this method unless the app is actually open. If you want to update the badge number upon receiving a notification then you need to set the Badge property of the json push notification to the desired number.

除非应用程序实际打开,否则它不会使用此方法更新徽章编号。如果您想在收到通知时更新徽章编号,则需要将 json 推送通知的 Badge 属性设置为所需的编号。

If you, if you are sending a normal message (not using json) there is a toggle to increment the badge number, just tick that. If you're using Json then use this:

如果您要发送普通消息(不使用 json),则可以通过切换来增加徽章编号,只需勾选即可。如果您使用的是 Json,请使用以下命令:

{
    "aps": {
        "alert": "Test Push Notification",
        "sound": "yourSound.aiff",
        "Badge": "desiredNumber"
    }
}

Please note, if you do not wish to send this from the server, you can also send it from one device to another utilising Parse's client push, go into your settings in the app on Parse.com and enable "client push", you can then send the above Json to another user's device.

请注意,如果您不想从服务器发送此信息,您也可以使用 Parse 的客户端推送将其从一台设备发送到另一台设备,进入 Parse.com 应用程序中的设置并启用“客户端推送”,您可以然后将上述 Json 发送到另一个用户的设备。

回答by Rizwan Shaikh

Whenever code is compiled it shows the badge icon which is previously store in your app. If you don't set the badge icon = 0 in your app it will show the badge icon number in your app every time you compile it or enter in background state.

无论何时编译代码,它都会显示先前存储在您的应用程序中的徽章图标。如果您没有在您的应用程序中设置徽章图标 = 0,它会在您每次编译或进入后台状态时在您的应用程序中显示徽章图标编号。

Now for your problem, use badge icon as

现在对于您的问题,使用徽章图标作为

var badgeCount = 0 

 UIApplication.sharedApplication().applicationIconBadgeNumber = ++badgeCount

Also whenever you are done with your task make badge icon as 0 otherwise it will show a badge icon in your app

此外,每当您完成任务时,将徽章图标设为 0,否则它会在您的应用中显示徽章图标

UIApplication.sharedApplication().applicationIconBadgeNumber = 0

回答by Rahul Krishna

I have worked on similar scenario and the final solution I found to increment and reset the badge numbers.

我已经研究过类似的场景和我发现的最终解决方案来增加和重置徽章编号。

Increment Badge number

递增徽章编号

  1. I always save the badge number count in the memory (NSUserDefaults)
  2. Every time i have to set the notification, I get the current badge number increment that and set that number on .applicationIconBadgeNumberand update the count in memory.
  1. 我总是将徽章编号计数保存在内存中 ( NSUserDefaults)
  2. 每次我必须设置通知时,我都会得到当前徽章编号的增量并设置该编号.applicationIconBadgeNumber并更新内存中的计数。

Reset Badge Number

重置徽章编号

  1. In my case, I have to reset all the badge count once the application is opened. So I have set UIApplication.sharedApplication().applicationIconBadgeNumber = 0in didFinishLaunchingWithOptionsof AppDelegate. Also I reset the count in the memory.
  1. 就我而言,一旦打开应用程序,我就必须重置所有徽章计数。所以,我已UIApplication.sharedApplication().applicationIconBadgeNumber = 0didFinishLaunchingWithOptionsAppDelegate。我也重置了内存中的计数。

回答by Diego Carrera

In Swift 5, you can update de application's badge whenever you want, using this code:

在 Swift 5 中,您可以使用以下代码随时更新应用程序的徽章:

UIApplication.shared.applicationIconBadgeNumber = 0 // YOUR NUMBER DESIRED

回答by Eric Johnson

None of these answers are valid anymore.

这些答案都不再有效。

You need to be looking at your Push code, not your AppDelegate

您需要查看您的推送代码,而不是您的 AppDelegate

From the Parse docs:

从解析文档:

badge: (iOS/OS X 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.