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

查看:150
本文介绍了更新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()
}

我确实尝试了下一个变体:初始化一个新的 var badgeCount = 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()
}

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

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

推荐答案

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

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天全站免登陆