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?
-
Give me actual use case you need to track? – Công Hải May 05 '20 at 05:01
-
@CôngHải I am having few datasets which changes periodically, say at a time interval less than 5 minutes. We show a pretty huge dataset size at once, with more than 3k data points at once. So the latest data should be available to the user as soon as he opens the app and be instantaneous while he is using it. – Aditya Kumar May 05 '20 at 10:16
1 Answers
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).
2- Data should be pushed to your app.
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.
In the second approach, In iOS operating system, while app is in foreground state, There are Silent Push Notifications and 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.
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)
You can read more about this and its limitations in Apple Documentations
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. 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.

- 206
- 1
- 7
-
Thanks a lot for the good answer. I had one more query related to pushing data using silent notifications. Would it be recommended to implement a heartbeat kind of mechanism to track the active users( FCM token) so I could send notifications(from server) notifying about updated data or the data updated itself to only currently active users who have the app open in the foreground. If yes, is there a library that I can use or what are the best practices for this heartbeat mechanism. – Aditya Kumar May 05 '20 at 10:22
-
As far as I know, there is no way to detect whether the app is active or not from backend point of view. But you can send the notification to all users, Then while processing the payload check for application state to determine whether it is in foreground or not, So decide for doing the tasks. For checking app state you can read more here [https://stackoverflow.com/questions/38971994/detect-if-the-application-in-background-or-foreground-in-swift] – Mahsa Yousefi May 05 '20 at 10:53