当 UNNotificationExtensionUserInteractionEnabled 为真时,如何从 UNNotificationContentExtension 触发 didReceiveRemoteNotification

How to trigger did didReceiveRemoteNotification from UNNotificationContentExtension when UNNotificationExtensionUserInteractionEnabled is true

背景:

我已经实现了 UNNotificationContentExtension 以便我可以格式化接收到的 APNS 数据并根据我的需要呈现它,如下所示

我希望用户点击星星并评分,所以我使用

UNNotificationContentExtension 上启用了用户交互
<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>UNNotificationExtensionCategory</key>
        <string>test</string>
        <key>UNNotificationExtensionDefaultContentHidden</key>
        <true/>
        <key>UNNotificationExtensionInitialContentSizeRatio</key>
        <real>1</real>
        <key>UNNotificationExtensionOverridesDefaultTitle</key>
        <false/>
        <key>UNNotificationExtensionUserInteractionEnabled</key>
        <true/>
    </dict>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.usernotifications.content-extension</string>
</dict>

有什么问题吗?

现在用户可以点击星星并对其进行评分,但是由于无论用户在通知视图的哪个位置都启用了用户交互,因此通知不会关闭,也不会将数据移交给父应用程序。因此,无论用户点击自定义 UI.

多少次,都不会调用 didReceiveRemoteNotification

我想达到什么目的?

我希望正常的 iOS 通知流程在用户点击任何星星或点击自定义 UI 中的任何位置时启动并将通知负载移交给 iOS 父应用程序。如果用户点击星号,我将传播所提供的评级,否则 0 将被通过。

UNNotificationExtensionUserInteractionEnabled 为真时,如何在用户点击自定义 UI 时关闭通知并触发父应用程序 didReceiveRemoteNotificationdidFinishLaunchingWithOptions

想通了 :) Apple 从 iOS 12 开始引入了 performNotificationDefaultAction()

根据文档

    // Opens the corresponding applicaton and delivers it the default notification action response
    @available(iOS 12.0, *)
    open func performNotificationDefaultAction()

所以我所要做的就是,一旦用户点击任何星星,我就必须以编程方式调用 performNotificationDefaultAction

@IBAction func ratingTapped(_ sender: UIButton) {
        debugPrint("\(sender.tag)")
        if #available(iOSApplicationExtension 12.0, *) {
            self.extensionContext?.performNotificationDefaultAction()
        } else {
            // Fallback on earlier versions
        }
    }

如果您想在用户点击自定义 UI 中的任何地方(不仅仅是星星)时将通知移交给父应用程序,您可以覆盖 hitTestpointInside 和 运行 同样的说法 :D

self.extensionContext?.performNotificationDefaultAction()

希望对以后遇到类似问题的人有所帮助:)