我試圖實現新的通知服務擴展,但我有一個問題。iOS 10不調用通知服務擴展
在我NotificationService.swift文件我有這樣的代碼:
class 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)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
print(bestAttemptContent.body)
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)
}
}
}
當我得到了一個推送通知的didReceive(_要求:UNNotificationRequest,withContentHandler contentHandler時:@escaping(UNNotificationContent) - >無效)方法從不叫。
也許我誤解了這個擴展是如何工作的?
嗨。 你解決了這個問題嗎? – user3703910