iOS 10不会调用Notification Service Extension [英] iOS 10 don't call Notification Service Extension
问题描述
我尝试实现新的通知服务扩展,但我遇到了问题。
I tried to implement the new Notification Service Extension, but I have a problem.
在我的NotificationService.swift文件中,我有以下代码:
In my NotificationService.swift file I have this code:
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(_ request:UNNotificationRequest,withContentHandler contentHandler:@escaping(UNNotificationContent) - > Void)从未调用的方法。
When I got a push notification the didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) method never called.
也许我误解了这个扩展是如何工作的?
Maybe I misunderstood how this extension is working?
推荐答案
在服务扩展上检查您的部署目标。
在使用10.1的设备上进行测试时我将部署目标设置为10.2,直到我才调用扩展改变了
I had deployment target set to 10.2 when testing on device with 10.1 and extension wasn't called until I changed it.
另一个问题可能是调试..为了使其工作您需要附加服务扩展流程。在Xcode菜单中,Debug> Attach To Process>扩展名称
Another issue might be debugging.. In order to make it work You need to attach Service Extension Process. In Xcode menu Debug > Attach To Process > Name of your extension
这篇关于iOS 10不会调用Notification Service Extension的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!