在 Swift 中更新徽章计数器 [英] Update badge counter in Swift

查看:20
本文介绍了在 Swift 中更新徽章计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我确实尝试了下一个变体:初始化了一个新的 varbadgeCount = 0 和更高版本:

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

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

但是当我收到新通知时,它不会更新为 +1.有人知道怎么解决吗?

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

推荐答案

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

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),则可以通过切换来增加徽章编号,只需勾选即可.如果您使用的是 Json,请使用以下代码:

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:

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

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

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.

这篇关于在 Swift 中更新徽章计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆