【问题标题】:macOS Notification Service Extension not being used未使用 macOS 通知服务扩展
【发布时间】: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


【解决方案1】:

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

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

请看附图。 Xcode Notification Services Extension target setting

【讨论】:

  • 第 5 点是错误的,会导致您的应用被拒绝。推送通知和通知服务扩展不需要传入连接。
【解决方案2】:

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

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

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

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

【讨论】:

    【解决方案3】:

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

    pluginkitthis SO thread 将我推向了正确的方向。

    【讨论】:

      猜你喜欢
      • 2022-08-03
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      相关资源
      最近更新 更多