[英]Is it recommended to use silent notification(FCM) to push data into mobile apps when the app is in Foreground?
Also is there an effective way to track, by the backend server, if the app being used by a user is in the foreground?还有一种有效的方法可以通过后端服务器跟踪用户正在使用的应用程序是否在前台? What are the best practices and recommended ways in mobile app development to get the latest data as soon as possible from the backend server to mobile apps being operated in the foreground?
在移动应用程序开发中,有哪些最佳实践和推荐方法可以尽快从后端服务器获取最新数据到在前台运行的移动应用程序?
Generally, for staying up to date, there are lots of solutions based on your case.通常,为了保持最新状态,根据您的情况有很多解决方案。 I'm going to divide them into two approaches:
我将它们分为两种方法:
1- You pull the new data when some specific conditions meet (eg; Intervally or based on user actions). 1- 当某些特定条件满足时(例如,间隔或基于用户操作),您会提取新数据。
2- Data should be pushed to your app. 2-数据应该被推送到您的应用程序。
The first approach is quite obvious if you need to be up to date at some intervals, you can call networks APIs at intervals and fetch the latest changes.如果您需要每隔一段时间更新一次,第一种方法非常明显,您可以每隔一段时间调用网络 API 并获取最新更改。
In the second approach, In iOS operating system, while app is in foreground state, There are Silent Push Notifications and WebSockets .在第二种方法中,在 iOS 操作系统中,当应用程序在前台 state 时,有静默推送通知和WebSockets 。
Silent push notifications is kind of push notification which can carry payloads and deliver to your app silently either your app is in background state or foreground.静默推送通知是一种推送通知,它可以携带有效负载并以静默方式传送到您的应用程序,无论您的应用程序位于后台 state 还是前台。 There is some limitations to this kind of notification, based on some conditions OS will decide to deliver push notifications to your app or not.
这种通知有一些限制,根据某些条件,操作系统将决定是否向您的应用程序发送推送通知。 and also there is payload size limitation (up to 4Kb)
并且还有有效载荷大小限制(高达 4Kb)
You can read more about this and its limitations in Apple Documentations您可以在Apple 文档中阅读有关此内容及其限制的更多信息
The second approach is pretty straightforward, It needs to use third-party libraries (like StarScream ) to open a socket connection and get the latest updates real-time.第二种方法非常简单,它需要使用第三方库(如StarScream )打开套接字连接并实时获取最新更新。 There is no limitations in this approach for times that server pushes data to your app or the size of payload you are receiving.
对于服务器将数据推送到您的应用程序的时间或您接收的有效负载的大小,这种方法没有限制。 so you can be notified from last changes by your backend server every moment while app is in foreground and connection is alive.
因此,当应用程序处于前台且连接处于活动状态时,您可以随时从后端服务器收到最新更改的通知。
Choosing between these two approaches is completely depends on your case.在这两种方法之间进行选择完全取决于您的情况。 If you should receive the update a lot of time in an hour and transferring lots of data so I recommend you the WebSocket approach, otherwise push notification will be sufficient and easier to implement.
如果您应该在一小时内收到大量更新并传输大量数据,那么我建议您使用 WebSocket 方法,否则推送通知将足够且更易于实施。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.