简体   繁体   English

如何从通知服务扩展访问应用程序的徽章计数

[英]How access app's badge count from Notification Service Extension

I want to increase the app's badge count when app receives any push notification even if the app is quit.我想在应用程序收到任何推送通知时增加应用程序的徽章计数,即使应用程序已退出。 The badge count is not maintained on server side.徽章计数不在服务器端维护。 So I need to update app badge count increase by 1 on receive every push notification.所以我需要在收到每个推送通知时将应用程序徽章计数增加 1。 The only way to modify push notification payload when app is in quit state is Notification Service Extension, But we can't access the UIApplication instance from extension to get app's current badge count.当应用程序处于退出状态时修改推送通知有效负载的唯一方法是通知服务扩展,但我们无法从扩展访问 UIApplication 实例来获取应用程序的当前徽章计数。

Is there any way to get app badge count from notification extension有没有办法从通知扩展中获取应用程序徽章计数

or Is there any other way to achieve this?或者有没有其他方法可以实现这一目标?

Please help me out from this.请帮我解决这个问题。 Thanks!谢谢!

没有 api 可以让您这样做,但您可以通过观察应用程序内用户默认密钥的环更改(您将在扩展程序中设置访问组套件默认值)并根据需要更改徽章来解决此问题。

You can set the app badge from the notification service extension by setting the given request's badge property...您可以通过设置给定请求的徽章属性从通知服务扩展设置应用程序徽章...

bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
bestAttemptContent = <current value +1>

If you can't compute the current value you could store it in UserDefaults如果您无法计算当前值,则可以将其存储在 UserDefaults

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

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