Handling push notifications while watchOS app is in the foreground?

天涯浪子 提交于 2019-12-03 20:47:27

Figured this out. To handle notifications when the watch app is either in background and foreground, it seems like you have to implement methods in both the didReceive method in the NotificationController (for notifications while in background) and the UNUserNotificationDelegate protocol in the ExtensionDelegate with the willPresent method (for notifications while in foreground):

func userNotificationCenter(_: UNUserNotificationCenter, willPresent: UNNotification, withCompletionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("willPresent fired from watch delegate")
}

What had confused me was that the docs say:

The User Notifications framework offers a unified API for use in iOS, watchOS, and tvOS apps, and supports most tasks associated with local and remote notifications. Here are some examples of tasks you can perform with this framework...

But while they go through this for iOS, tvOS, and macOS, the docs don't specify exactly how do this for the watch.

Going through the registration and device token steps is not necessary in the Watch ExtensionDelegate (and actually impossible because you need the UIApplication module to do it), but it is necessary to invoke:

UNUserNotificationCenter.current().delegate = self

upon launching the ExtensionDelegate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!