【发布时间】:2019-12-18 10:07:31
【问题描述】:
我的 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)
}
}
}
我的payload也很简单:
{"aps": {
"alert":{"title":"Test1", "subtitle":"Test2", "body":"Test3"},
"sound":"default",
"mutable-content":1,
"category":"news"
}
}
但是,在收到通知后,标题并没有被修改。我也尝试了Attach to process by PID or name 菜单,我无法附加到这个扩展,这意味着它没有被运行。
许多其他问题都在询问有关 iOS,我尝试了这些解决方案,但不幸的是它们不起作用。
有什么想法吗?
【问题讨论】:
-
您正在使用 Catalyst 应用程序/MacOS Catalina?
-
对标识符使用反向域表示法。在 Mac 上,这几乎是必需的,否则会出现奇怪的行为
-
您找到任何解决方案了吗?我也无法让通知服务工作。我正在使用 macOS 10.15 开发可可应用程序。
-
你有没有找到调试催化剂版本的解决方案?
标签: macos notifications unnotificationserviceextension