gpt4 book ai didi

ios - WatchKit 2.0 检测何时收到 APNS

转载 作者:搜寻专家 更新时间:2023-11-01 05:57:27 25 4
gpt4 key购买 nike

我正在创建一个具有聊天功能的 Apple Watch 应用程序。我想在 watch 应用程序运行并收到 APNS 消息时更新聊天对话。我知道在 AppDelegate 中,函数

application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) 

可以使用,但只有当设备应用程序在前台而不是后台运行时才可以使用。在WKUserNotificationInterfaceController 中,有函数didReceiveRemoteNotification,但它只在使用动态通知时使用。是否可以让 watch 应用程序处理在不使用动态通知时收到并在前台运行的远程通知?

最佳答案

在创建 WatchKit 应用程序和使用 APNS 时,关于推送通知在 Watch 端的实际工作方式,您需要了解一些事情。

1) iOS 系统根据用户事件(iPhone 锁定/解锁; watch 使用/未使用...)选择通知是否由 Watch 处理。

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 类中有 didReceiveRemoteNotification(_:),它会在您的 WatchKit 应用程序运行时调用。参见 here获取完整文档。

如果您需要在运行时通过 APNS 更新您的应用程序,您应该使用最后一种方法处理它;)

希望对你有帮助!

关于ios - WatchKit 2.0 检测何时收到 APNS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35850548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com