未使用macOS通知服务扩展

问题描述 投票:1回答:1

我的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"
    }
}

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

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

有什么想法吗?

macos notifications unnotificationserviceextension
1个回答
0
投票

我对Mac Catalyst有相同的问题(iOS Notification Services扩展正常,但不适用于MAC OS)。 (Xcode 11.1)对于Mac OS X(Mac Catalyst)(如果您已经具有“应用程序沙箱”功能,请跳至步骤5)

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

    请参阅附件图像。Xcode Notification Services Extension target setting

© www.soinside.com 2019 - 2024. All rights reserved.