菜鸟教程小白 发表于 2022-12-11 19:16:01

ios - 无法在 Apple Watch 上获得可操作通知回调


                                            <p><p>我有一个推送通知的 iOS 应用程序(例如当用户进入一个地理区域时)。
我还有一个 watch 应用程序,可以在 iPhone 锁定时收到这些通知。我在这些通知中有 2 个操作(“调用”、“转到”),它们正确显示在 Apple Watch 通知中,但是当用户触摸其中一个操作时,回调</p>

<pre><code>userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -&gt; Void)
</code></pre>

<p>我的 <strong>UNUserNotificationCenterDelegate</strong> 永远不会被调用!</p>

<p>这是 iOS 应用程序中的代码:</p>

<pre><code>func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -&gt; Bool {

    // Register local notification (alert &amp; sound)
    // They are used to notify the user when entering/exiting a monitored region
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound], completionHandler: { (granted, error) in
      if let theError = error {
            NSLog(&#34;\(#function) error when request localNotification \(theError.localizedDescription)&#34;)
      } else {
            if !granted {
                NSLog(&#34;\(#function) Warning local notification not granted&#34;)
            }
      }

   })

    var actions = ()
    actions.append(UNNotificationAction(identifier: &#34;CallId&#34;, title: &#34;Call&#34;, options:[] )) // UNNotificationActionOptions.authenticationRequired
    actions.append(UNNotificationAction(identifier: &#34;CallId2&#34;, title: &#34;Go To&#34;, options: ))

    let notificationCategory = UNNotificationCategory(identifier: &#34;MonitoringRegionCategory&#34;, actions: actions, intentIdentifiers: [],options: [])
    let categories: Set =
    UNUserNotificationCenter.current().setNotificationCategories(categories)
    return true
}
</code></pre>

<p>发送通知的代码(在 iOS 应用中)</p>

<pre><code>    let content = UNMutableNotificationContent()
    content.title = &#34;title&#34;
    content.subtitle = &#34;subtitle&#34;

    var message:String
    if isEntering {
      message = String(format: NSLocalizedString(&#34;POI less than %d meters&#34;, comment: &#34;&#34;), Int(poi.poiRegionRadius))
    } else {
      message = String(format: NSLocalizedString(&#34;POI more than %d meters&#34;, comment: &#34;&#34;), Int(poi.poiRegionRadius))
    }

    content.body = message
    content.badge = 1
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = &#34;MonitoringRegionCategory&#34;
    let request = UNNotificationRequest(identifier: AppDelegate.LocalNotificationId.monitoringRegionId, content:content, trigger: nil)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
      if let theError = error {
            NSLog(&#34;\(#function) Error with notification add \(theError.localizedDescription)&#34;)
      }
    })
</code></pre>

<p>在WatchApp中以代码注册并实现UNUserNotificationCenterDelegate</p>

<pre><code>class ExtensionDelegate: NSObject, WKExtensionDelegate, UNUserNotificationCenterDelegate {

    func applicationDidFinishLaunching() {
      NSLog(&#34;\(#function)&#34;)


      // Perform any final initialization of your application.
      UNUserNotificationCenter.current().delegate = self
}

// MARK: UNUserNotificationCenterDelegate
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -&gt; Void) {
      NSLog(&#34;\(#function)&#34;)

      completionHandler()
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -&gt; Void) {
      NSLog(&#34;\(#function)&#34;)
      completionHandler(.alert)
    }
</code></pre>

<p>知道为什么当用户从通知中选择一个按钮时从不调用 userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) 吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我遇到了同样的问题,我通过将 <code>UNNotificationAction</code> 添加为 <code>.foreground</code> 类别来解决它。您必须更改以下行来修复它:</p>

<pre><code>var actions = ()
actions.append(UNNotificationAction(identifier: &#34;CallId&#34;, title: &#34;Call&#34;, options:[.foreground]))
actions.append(UNNotificationAction(identifier: &#34;CallId2&#34;, title: &#34;Go To&#34;, options:[.authenticationRequired]))
</code></pre>

<p><code>.foreground</code> 将启动您的应用程序并调用 <code>func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)</code> 方法.您可以在此方法中为可操作按钮处理您的操作和重定向。示例如下:</p>

<pre><code>func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -&gt; Void) {
   switch response.actionIdentifier.lowercased() {
      case &#34;call&#34;:
            WKInterfaceController.reloadRootControllers(withNames: [&#34;CallInterface&#34;], contexts: nil)
      default: break
   }
   completionHandler()
}
</code></pre>

<p>希望对你有帮助!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法在 Apple Watch 上获得可操作通知回调,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47211564/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47211564/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法在 Apple Watch 上获得可操作通知回调