问题描述
我正在尝试使用UNNotificationServiceExtension实现应用程序徽章解决方案。当我实施基本解决方案时。它显示的是[modified]标题,但是当我添加应用程序分组解决方案并尝试访问UserDefaults时,它将停止工作
import UserNotifications
NotificationService类:UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest,withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
let defaults = UserDefaults(suiteName: "group.hu.focifc.focifc")
var count: Int = defaults!.value(forKey: "count") as! Int
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
//bestAttemptContent.badge = count as? NSNumber
//count = count + 1
//defaults?.set(count,forKey: "count")
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content,otherwise the original push payload will be used.
if let contentHandler = contentHandler,let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
当我删除或取消注释... var count:Int ...行时,它没有完成工作。
有什么主意吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)