简体   繁体   English

重置图标徽章上的计数

[英]Reset count on icon badge

I send pushes using Parse.com. 我使用Parse.com发送推送。 When i send pushes i set increment badge to "yes" so users can see badges with "1" on my app icon. 当我发送推送时,我将增量徽章设置为“是”,以便用户可以在我的应用程序图标上看到带有“ 1”的徽章。

When users opens app's main ViewController, app clears badge count using this code 当用户打开应用程序的主ViewController时,应用程序将使用以下代码清除徽章计数

UIApplication.sharedApplication().applicationIconBadgeNumber = 0

This works fine and badge is clear. 这可以正常工作,并且徽章很清楚。 But when i send new push with badge increment enabled - i see that number on badge is 2. Is something wrong with my way to reset badge count? 但是,当我在启用徽章增量的情况下发送新推送时-我看到徽章上的数字是2。我重置徽章计数的方式出了问题吗?

That's because while you remove the local badge, the badge count in Parse Installation class for that device remains the same. 这是因为当您删除本地标志时,该设备的“解析安装”类中的标志计数保持不变。 You can do the following to remove that: 您可以执行以下操作将其删除:

Assuming that you have already made sure that current user has a PFInstallation (ie he did not decline the push notification access request) , To reset the badge number on backend, you can use the following: 假设您已经确定当前用户具有PFInstallation(即,他没有拒绝推送通知访问请求),要在后端重置徽章编号,可以使用以下命令:

var currentInstallation = PFInstallation.currentInstallation()
      if currentInstallation.badge != 0 {
        currentInstallation.badge = 0
        currentInstallation.save
       }

This makes sure to set the badge to 0 only if it is currently showing a non-zero counter. 这样可以确保仅在徽章当前显示非零计数器时才将徽章设置为0。

Setting badge on currentInstallation will automatically set applicationIconBadgeNumber too. currentInstallation上设置徽章也会自动设置applicationIconBadgeNumber。 By doing this, Parse will know what number your app is currently displaying and they can increment the counter correctly whenever an Increment is sent to this device again. 这样,Parse将知道您的应用程序当前正在显示的数字,并且每当再次将增量发送到该设备时,它们就可以正确地递增计数器。 For more information you can read Official Parse Blog Announcement on this matter. 有关更多信息,您可以阅读有关此问题的官方解析博客公告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM