菜鸟教程小白 发表于 2022-12-11 19:28:55

ios - 向 UNNotificationCategory 添加动态操作


                                            <p><p>我目前面临一个问题,我需要能够根据随通知发送的信息更改 UNNotificationCategory 的操作。目前我的类别设置与此类似:</p>

<pre><code>func registerNotificationSettings(){
    let acceptAction = UNNotificationAction(identifier: &#34;accept&#34;, title:&#34;accept&#34;, options: [])
    let denyAction = UNNotificationAction(identifier: &#34;deny&#34;, title: &#34;deny&#34;, options: [])

    let acceptCategory = UNNotificationCategory(identifier: &#34;2&#34;, actions: , intentIdentifiers: [], options: [])
    let denyCategory = UNNotificationCategory(identifier: &#34;1&#34;, actions: , intentIdentifiers: [], options: [])

    let center = UNUserNotificationCenter.current()
    center.setNotificationCategories()

    center.requestAuthorization(options: [.alert, .badge, .sound]){
       (granted, error) in
      if (granted){
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
      }
    }
}
</code></pre>

<p>此配置有效,但是,我需要能够根据用户设置更改每个类别的默认操作。例如,“denyAction”中的“deny”可能需要改为“reject”,或者“acceptAction”中的“accept”可能需要改为“confirm”。这些设置是我们为用户在应用程序中指定的变量而设置的,我无法知道他们可能会将其更改为什么。</p>

<p>目前我们在每个通知的 userInfo 中都有发送“响应”字典的通知,但我不知道如何在收到通知时更改通知操作。我发现的唯一文档提前设置了这些操作。有谁知道这是否可能?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我能够通过创建一个方法来解决这个问题,该方法从我的服务器请求可能的状态列表,其中包括与该状态相关的操作。然后我为每个状态创建一个 UNNotificationCategory 并为每个 Action 创建一个 UNNotificationAction。每次应用程序运行时我都会调用此方法。这不是一个完美的解决方案,因为如果有人从不强制退出应用程序,并且状态数据发生变化,那么我就不会得到新数据。</p>

<p>这是我的一些代码</p>

<pre><code>for state in notificationStates {

            var notificationActions : = []
            for action in state.actions {
                let notificationAction = UNNotificationAction(identifier: action, title: action, options: [])
                notificationActions.append(notificationAction)
            }

            let muteAction = UNNotificationAction(identifier: &#34;mute&#34;, title: &#34;Mute&#34;, options: [])
            let commentAction = UNTextInputNotificationAction(identifier: &#34;inline-comment&#34;, title: &#34;Add Comment&#34;, options: [], textInputButtonTitle: &#34;Add Comment&#34;, textInputPlaceholder: &#34;Comment&#34;)

            notificationActions.append(muteAction)
            notificationActions.append(commentAction)

            let notificationCategory = UNNotificationCategory(identifier: state.id, actions: notificationActions, intentIdentifiers: [], options: [])
            notificationCategories.insert(notificationCategory)
      }
      center.setNotificationCategories(notificationCategories)
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 向 UNNotificationCategory 添加动态操作,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47891203/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47891203/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 向 UNNotificationCategory 添加动态操作