WatchKit 2.0 检测何时收到 APNS
WatchKit 2.0 Detect When APNS Is Received
我正在创建一个具有聊天功能的 Apple Watch 应用程序。每当手表应用 运行 并且收到 APNS 消息时,我想更新聊天对话。我知道 AppDelegate
中的函数
application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
可以使用,但只有当设备应用程序 运行 在前台而不是后台时才可以使用。在WKUserNotificationInterfaceController
中有函数didReceiveRemoteNotification
,但只有在使用动态通知时才会用到。是否可以让手表应用程序处理不使用动态通知时收到的远程通知,并且 运行 在前台?
创建 WatchKit 应用程序并使用 APNS 时,您需要了解推送通知在 Watch 端的实际工作方式。
1) iOS系统根据用户activity选择通知是否由Watch处理(iPhone locked/unlocked; Watch是used/not 使用...)。
When one of your app’s local or remote notifications arrives on the user’s iPhone, iOS decides whether to display that notification on the iPhone or on the Apple Watch. See Doc Apple
2) 对于 iOS 端,您可以在 didReceiveRemoteNotificationapplication(_:didReceiveRemoteNotification:fetchCompletionHandler:)
中处理所有通知 ==> 您可以检查您的应用是否在前台 + 处理静默通知。
3) Watch端有两个不同的通知入口点:
- 如您所说,
WKUserNotificationInterfaceController
是在您的 WatchKit 应用未被使用时调用的。
- 你的
ExtensionDelegate
class 中有 didReceiveRemoteNotification(_:)
,当你的 WatchKit 应用程序是 运行 时调用它。有关完整文档,请参阅 here。
如果您的应用在 运行 期间需要通过 APNS 更新,您应该使用最后一种方法进行处理 ;)
希望对您有所帮助!
我正在创建一个具有聊天功能的 Apple Watch 应用程序。每当手表应用 运行 并且收到 APNS 消息时,我想更新聊天对话。我知道 AppDelegate
中的函数
application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
可以使用,但只有当设备应用程序 运行 在前台而不是后台时才可以使用。在WKUserNotificationInterfaceController
中有函数didReceiveRemoteNotification
,但只有在使用动态通知时才会用到。是否可以让手表应用程序处理不使用动态通知时收到的远程通知,并且 运行 在前台?
创建 WatchKit 应用程序并使用 APNS 时,您需要了解推送通知在 Watch 端的实际工作方式。
1) iOS系统根据用户activity选择通知是否由Watch处理(iPhone locked/unlocked; Watch是used/not 使用...)。
When one of your app’s local or remote notifications arrives on the user’s iPhone, iOS decides whether to display that notification on the iPhone or on the Apple Watch. See Doc Apple
2) 对于 iOS 端,您可以在 didReceiveRemoteNotificationapplication(_:didReceiveRemoteNotification:fetchCompletionHandler:)
中处理所有通知 ==> 您可以检查您的应用是否在前台 + 处理静默通知。
3) Watch端有两个不同的通知入口点:
- 如您所说,
WKUserNotificationInterfaceController
是在您的 WatchKit 应用未被使用时调用的。 - 你的
ExtensionDelegate
class 中有didReceiveRemoteNotification(_:)
,当你的 WatchKit 应用程序是 运行 时调用它。有关完整文档,请参阅 here。
如果您的应用在 运行 期间需要通过 APNS 更新,您应该使用最后一种方法进行处理 ;)
希望对您有所帮助!