3

我的 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,我尝试了这些解决方案,但不幸的是它们不起作用。

有任何想法吗?

4

3 回答 3

0

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

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

请参阅附图。 Xcode Notification Services Extension 目标设置

于 2019-10-17T19:09:00.170 回答
0

检查您是否没有安装多个版本的应用程序。在我删除了应用程序的发布版本(相同的捆绑 ID)后,一切都开始工作了。

pluginkit这个 SO 线程将我推向了正确的方向。

于 2021-07-26T11:12:05.993 回答
0

1- 没有运行应用程序扩展程序(或似乎没有运行)的另一种可能性是发生了一些事情并且扩展程序确实失败/崩溃并且操作系统在没有更改的情况下发送了推送通知。(尝试使用 Xcode 调试您的扩展。)

2- 也尝试在 Xcode 中清理您的目标和派生数据。

3- 确保您的扩展程序的权利具有所有支持的 Mac (Mac Catalyst) 而不仅仅是支持的 iOS 密钥(如果用于 Mac Catalyst)。

4-(Mac Catalyst)尝试在 iOS 上运行扩展程序(如果在 Mac 上运行但不在 Mac 上,则意味着某些配置/代码崩溃不应该出现在您的 Mac 目标中)再次调试应该会有所帮助

于 2019-11-04T17:56:44.983 回答