未使用 macOS 通知服务扩展

Tom*_*hen 6 macos notifications unnotificationserviceextension

我的 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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的有效负载也非常简单:

{"aps": {
        "alert":{"title":"Test1", "subtitle":"Test2", "body":"Test3"},
        "sound":"default",
        "mutable-content":1,
        "category":"news"
    }
}
Run Code Online (Sandbox Code Playgroud)

但在收到通知后,标题并未修改。我也尝试了Attach to process by PID or name菜单,我无法附加到此扩展,这意味着它没有被运行。

许多其他问题都在询问有关 iOS 的问题,我尝试了这些解决方案,但不幸的是它们不起作用。

有任何想法吗?

小智 -1

我在Mac Catalyst上也遇到了同样的问题(iOS 通知服务扩展工作正常,但不适用于 MAC 操作系统)。( Xcode 11.1 ) 对于 Mac OS X (Mac Catalyst)(如果您已经具有“应用程序沙箱”功能,请跳至步骤 5)

  1. 选择您的扩展程序的目标。
  2. 然后在顶部选择选项卡“签名和功能
  3. 而不是点击“能力+”
  4. 添加“应用沙箱功能
  5. 确保选中(启用)传入连接(服务器)

请参阅附图。 Xcode 通知服务扩展目标设置