我有一个针对macOS应用的通知服务扩展。
以下是该扩展的代码:
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
print("Extension received notification!")
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
bestAttemptContent?.title = "Title modified!"
}
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)
}
}
}
我的负载也非常简单:
{"aps": {
"alert":{"title":"Test1", "subtitle":"Test2", "body":"Test3"},
"sound":"default",
"mutable-content":1,
"category":"news"
}
}
然而,收到通知后,标题没有被修改。我还尝试了按PID或名称附加到进程
菜单,但无法附加到此扩展程序,这意味着它没有在运行。
许多其他问题都涉及iOS,我尝试了这些解决方案,但不幸的是它们不起作用。
有什么想法吗?