macOS通知服务扩展未被使用。

6

我有一个针对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"
    }
}

然而,收到通知后,标题没有被修改。我还尝试了按PID或名称附加到进程菜单,但无法附加到此扩展程序,这意味着它没有在运行。

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

有什么想法吗?


你正在使用Catalyst应用程序/ MacOS Catalina吗? - dcrow
使用反向域名表示法来命名标识符。在 Mac 上几乎是必需的,否则会出现奇怪的行为。 - quellish
你找到任何解决方案了吗?我也无法让通知服务正常工作。我正在开发一个适用于macOS 10.15的Cocoa应用程序。 - francisfeng
你是否找到了调试催化剂版本的解决方案? - janh
4个回答

0
你的通知目标支持Mac吗?我遇到了这个问题。

Notification settings

看起来通知没有运行,你的推送通知由系统处理。

0

1- 另一种可能性是应用扩展未运行(或似乎未运行)的原因是发生了某些事情,导致扩展失败/崩溃,操作系统在没有更改的情况下传递了推送通知。(尝试使用Xcode调试您的扩展。)

2- 还可以尝试在Xcode中清除目标和派生数据。

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

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


0

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

pluginkitthis SO thread将我引导到了正确的方向。


-1

我遇到了同样的问题,使用 Mac Catalyst(iOS 通知服务扩展正常工作,但不适用于 MAC OS)。 (Xcode 11.1) 对于 Mac OS X(Mac Catalyst)(如果您已经拥有“应用程序沙盒”功能,请跳转到第5步)

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

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


1
第五点是错误的,会导致您的应用被拒绝。传入连接对于推送通知和通知服务扩展程序的工作并非必需。 - sudoExclaimationExclaimation

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接