0

My plan is to enable drops (basicly chests people can open) inside my app, and I want to generate this chests every day at specific times, for example every 3 hours. I want to acomplish this with calling a function every 3 hours which will make the chests appear, so all I need to do is basicly call one function at specific times a day.

The problem I face here is that this is my first app and I dont really know how to call such a function at specific times a day, and even less so how to call this function while the app is in background or even closed.

I read that there are possibilites to make an app do something while beeing in background but as soon as its terminated it is not working anymore - On the other side we have apps like whatsapp or facebook or basicly any messenger which give you notifications even if you terminate those apps, so Im really confused. I would like to make the chest appear all 3 hours even if the app was terminated and not let the user disappointed getting back in the app and noticing there is no drop because he terminated the app.

Does anybody know the approach to this or any tips how I could acomplish it?

PS: I also want to include expo-notifications at some point, just as a sidenote incase this info should help.

  • 1
    The endless background process even if you kill the app is something that hasn't really been available for react-native but this [article](https://www.qed42.com/blog/react-native-endless-background-process) shows how it is possible to accomplish it. Though, I would probably just run a server that would send notifications to the app every 3 hours to remind the player and then make requests to your server when the app is in foreground to determine how many chests the player is allowed to see. – kemicofa ghost Jun 21 '21 at 18:38
  • 1
    This answer talks about how to set up an internal timer notification https://stackoverflow.com/questions/47259935/react-native-showing-notification-after-a-specific-intervals-of-time-ios . Maybe might answer your question. – kemicofa ghost Jun 21 '21 at 18:39
  • @kemicofaghost luckily I just noticed im not dependend on the app beeing killed or not, but I still checked the article and the post you linked, interesting info! If my app gets killed, there will be no notifications but I will just drop the chests as soon as app runs again and my app dectects that 3 hours have passed since last time. – Kubaghetto the fresh Testobun Jun 22 '21 at 23:38

1 Answers1

1

So Basically you want a chest in every 3 hour.

In react native you can call a function in background state but you can't call a function when app is in kill state.

To get notifications in kill state you can use push-notifications they work quit well and you can perform some function on opening of that notification.

For the logic of the chests every 3 hour I will suggest you to use a persist state management like redux-persist and store the time of creation of first chest in state and then whenever the user opens the app again, you get this stored time of chest creation and substract it from the current time and get the hours passed after the first chest creation and then divide it by 3(for 3 hours), this will basically give you the no of new chest you will have to create for the user.

Do this task in lifecycle methods in useEffect() or componentDidMount().

noOfNewChestToBeCreated=(previousChestCreationTimeDate-CurrentTimeDate)/3

here you can find how to how to substract date time in js here

Instead of redux-persit you can also use async-storage

Naman
  • 80
  • 8
  • Solved it with async-storage as suggested. Im saving the last time chest were dropped and when they are beeing dropped, I also set a notification for x time. After x time notification gets triggered and executes the function to check if time passed and drops chests again and sets timer again etc. Tried it with x=60 Secounds and it works perfect, should be also good to work with 2 hours, thank you! – Kubaghetto the fresh Testobun Jun 22 '21 at 23:35