gpt4 book ai didi

iOS 10 不调用通知服务扩展

转载 作者:IT王子 更新时间:2023-10-29 05:22:11 25 4
gpt4 key购买 nike

我尝试实现新的通知服务扩展,但遇到问题。

在我的 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(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) 方法从未被调用。

也许我误解了这个扩展的工作原理?

最佳答案

检查您在服务扩展上的部署目标。

在使用 10.1 的设备上进行测试时,我将部署目标设置为 10.2,并且在我更改它之前不会调用扩展。

另一个问题可能是调试......为了使其正常工作,您需要附加服务扩展进程。在 Xcode 菜单 Debug > Attach To Process > Name of your extension

关于iOS 10 不调用通知服务扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39663315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com