gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:42:28 25 4
gpt4 key购买 nike

背景:

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

enter image description here

我希望用户点击星星并评分,所以我在 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>

有什么问题?

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

我想实现什么?

我希望当用户点击任何星标或点击自定义 UI 中的任何位置时,正常的 iOS 通知流程启动并将通知负载移交给 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()

希望它能帮助以后遇到类似问题的人:)

关于ios - 当 UNNotificationExtensionUserInteractionEnabled 为真时,如何从 UNNotificationContentExtension 触发 didReceiveRemoteNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56128878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com