How do I respond to an app state change? (React Native) - javascript

I want to save the state of my app when the system decides to kill it randomly (when for example it was in the background for a long time). How do I intercept the OS's request to shutdown before my state is lost?

React-native provides an API called AppStateIOS which allows you to get notified when your app switches states. It doesn't provide notifications for all of the app lifecycle state changes, but it does let you know when your app goes to background.
The background state should be enough. When your app goes to the background it gets suspended (assuming you are not using background tasks or background services), and in this state, the OS can terminate your app at any moment if it needs more resources. You won't be able to know when your app gets terminated, so when it goes to the background - this is the point where you should save your state in case your apps gets terminated later.

Related

Push notification when app is in foreground (using Firebase triggers)

Let's say that there is an update in certain collection and it triggers function that "exports" notification for certain user (token). I have implemented Firebase Functions in JavaScript for Firestore Triggers (onCreate, onUpdate,...) and it works if app is in background.
I want to send a notification whether the application is running or not...
Can user receive notification (lets say onUpdate) if app is in foreground or that feature is not ready yet (since triggers are in beta version)?
Thanks in advance
If depends on whether your message contains a data property, a notification property, or both.
If the message only contains a notification property, it'll delivered to your application code if the app is in the foreground, but handled by the system when the app is in the background.
If your message contains a data property, it'll always be delivered to your application code. If it also contains a notification property, that part will be handled by the system when the app is in the background.
For more on this, see the Firebase documentation on message types.

Last Method (or function) before app closes in react-native

I have some data which I want to keep for the duration of the session i.e when the app starts the data loads and can be accessed any time without fetching it again and when the app closes I want to delete the data so that when it restarts I have fresh data
Why not just flush the data when the app starts? You're not guaranteed to know when the app is killed and/or if the JS thread is running.
Your app could exit when fully in a hibernate state, which means the JS thread will not be executing.

React Native display message at certain time when app is disabled

How is it possible to have React Native trigger some kind of message on the device at a certain time, even if the app is currently not selected or closed down altogether?
I would simply use a push notification. This is something you will need to setup server side but it sounds like you have a good use case. You will need to setup permissions etc for this.
Maybe also a good use case for local notifications take a look at React Native Docs
A small completion to #JamesWatling's answer. You'll need at least:
AppState (https://facebook.github.io/react-native/docs/appstate.html#content) to post the timestamp to server, when app is moved to background
Push Notifications (e.g. https://onesignal.com/ or for iOS: https://facebook.github.io/react-native/docs/pushnotificationios.html#content) to send a notification after certain amount of time
You might want to have a look at this part of the react native documentation and focus on local notifications. You don't need a server or anything to schedule a notification at a given time, a local notification is scheduled without a server interfering.
I do believe your app is going to need to have permissions for push notifications though (so the app is allowed to display a message on a homescreen for instance)

React Native JS doesn't execute while app is in background

I am trying to get push notifications working in a ios React Native app and am half way there. The one problem im having is that my logic for displaying a Notification while the app is in the background doesn't execute until the app becomes active again. I am using Socket.io and listening for events, but the code to handle (say onChat event) doesn't run until the app is active. I have verified that the socket is still connected.
The one things thats weird is that is works in the simulator but not on my device. Maybe there is performance optimizations that halt JS execution while the app is put in the background.
How can i get my JS to execute while the appstate is in the background?
What im doing is calling a function when the event is recieved over the socket and it runs:
PushNotificationIOS.presentLocalNotification({
alertBody: `New Chat: ${chat.message}`,
});
Thanks to this stack overflow question and answers I was able to find out that code is not executed in the background state to prepare it for the suspended state. The background mode is a transition state to prepare the app to be "screen shotted". The socket will stay connected only for a short time there after, but would eventually get terminated.
In the case with doing a push notifications, I would need to turn that over to Parse or some other service to push a notification to my app while the app is in the background.
Also see http://zeroheroblog.com/ios/what-to-do-when-your-iphone-app-moves-to-the-background

Saving state of webpage with socket.io

I am creating a card game with node.js as the server , and i'm using web sockets (socket.io) to transfer the data from server to client .
so after the cards are dealt i want if someone refreshes the page he can see the current state of the game , in my case he just see nothing the cards are not dealt and there are no players . so is there some way to save the state of the game whenever a player refreshes the pages he can see all the changes that happen to the html page .
One way you can do it is to have the actual game state on the server. That way, when the user reloads the page, the page simply requests the state back from the server. This basically means that the game is actually on the server, and your clients are merely "remote controls" to the game on the server.
Another way is to save the state locally, using local storage. However, there could have been changes between the last time the user was on the game and during his return (like a card dealt, or a card drawn, a card passed etc.).
You can even use both. Where you read the local storage for the state first. That way, you have your hand state before you left. You can then request the server for the changes, and animate the game accordingly.

Categories

Resources