菜鸟教程小白 发表于 2022-12-11 19:44:51

ios - 关闭 UserNotificationsUI - UNNotificationContentExtensionResponseOption


                                            <p><p>我正在设置多个本地通知,并且我已将操作数设置为通知。像打盹、显示、关闭。 </p>

<pre><code>    func didReceive(_ response: UNNotificationResponse, completionHandler completion:@escaping (UNNotificationContentExtensionResponseOption) -&gt; Void) {


    let id = response.notification.request.identifier

       if response.actionIdentifier == &#34;SnoozeId&#34; {
      completion(UNNotificationContentExtensionResponseOption.dismiss)

         }}
</code></pre>

<p>在 didReceive - 如果操作是贪睡 - 执行贪睡,然后我关闭通知。当我解雇所有其他通知时,通知中心都消失了。如果我有两个 Notification A 和 B 。
如果我长按并在 A 上执行贪睡。 A 和 B 都从通知中心消失了。它应该只解雇 A。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>以下是使用我的应用进行测试的结果。 </p>

<p>为了更好地回答您的问题,请提供更多信息,例如 - 通知的 json 及其 userInfo(也许您正在对通知进行分组并一次全部关闭?)以及您调用的函数会导致调用didReceive(...).</p>

<p>如果你打电话</p>

<pre><code>self.extensionContext?.dismissNotificationContentExtension()
</code></pre>

<p>然后它关闭内容扩展但不关闭它。</p>

<p>如果你打电话:</p>

<pre><code>self.extensionContext?.performNotificationDefaultAction()
</code></pre>

<p>然后它完成操作并关闭该单个通知(而不是其他通知)。但是,这会打开应用程序,您可能不想这样做。</p>

<p>我的委托(delegate)是这样设置的:</p>

<pre><code>func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -&gt; Void) {

    switch response.actionIdentifier {
    case UNNotificationDismissActionIdentifier:
      // Dismiss action stuff.
      completion(.dismiss)
      // To not dismiss
      // completion(.doNotDismiss)
    case UNNotificationDefaultActionIdentifier:
      // Default action stuff.
      // Let&#39;s say the user executed default action, we want to dismiss and forward the action.
      completion(.dismissAndForwardAction)
      break
    default:
      break
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 关闭 UserNotificationsUI - UNNotificationContentExtensionResponseOption,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48800313/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48800313/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 关闭 UserNotificationsUI - UNNotificationContentExtensionResponseOption