当应用程序关闭或 phone 关闭时,Android/iOS 应用程序是否可以接收 Pusher 消息

Can Pusher messages be received on Android/iOS app when the app is closed or phone is turned off

Pusher 应用程序关闭或 phone 关闭后,是否可以在 Android/iOS 应用程序上接收消息?

我希望发生什么...

  1. 当 phone 开启且应用程序开启时,从 Pusher 文档中可以非常直接地了解如何对发送到应用程序的推送消息做出反应。

  2. 当 phone 开启且应用程序关闭时,phone 应该会收到通知,告知应用程序已发送内容。

  3. 当 phone 关闭时,phone 应该会在它重新打开时收到一条通知,通知已将某些内容发送到应用程序。

这些甚至可能吗?如果是,有哪些值得关注的建议?如果不是,有什么解决方法?

当使用 Pusher 的应用程序在 iOS 和 Android 进入后台时,与 Pusher 的连接可能会保持活动状态几分钟。但最终该连接将被关闭。您无法阻止在您的应用程序中发生这种情况(尽管在提交到 Apps 商店时可能会 "hack" 声明您的应用程序是语音应用程序)。

因此,您应该监控您的应用程序是否移至后台,并在 phone 未连接时将消息传递到适当的位置。

请参阅 How get pusher events when the iOS app go to the background? 了解 libPusher(Pusher iOS 库)。

这是 "Service Process" 上 Android Processes & Threads Lifecycle docs 的摘录:

A process that is running a service that has been started with the startService() method and does not fall into either of the two higher categories. Although service processes are not directly tied to anything the user sees, they are generally doing things that the user cares about (such as playing music in the background or downloading data on the network), so the system keeps them running unless there's not enough memory to retain them along with all foreground and visible processes.

因此,运行在这样的线程中使用 Pusher 实例是一个可行的选择。

但是,还是会有用户掉线的情况。在这种情况下,您的服务器可以通过 querying the Pusher HTTP API or by using WebHooks 检测用户是否在线,如果用户离线或恢复在线,您的服务器可以立即得到通知。

使用上面的机制可以方便smart notifications;如果用户在线(应用程序在前台),则通过 Pusher 向他们发送消息,如果他们离线(应用程序在后台),则使用替代机制进行传递(本机推送通知、短信或电子邮件) .