mathias5r / rn-heartbeat

MIT License
100 stars 36 forks source link

React native JS Engine and Foreground Service #35

Open akashapps opened 2 years ago

akashapps commented 2 years ago

I am working in similar application where we need to make sure the application stays alive in the background. We are using foreground service for it. (Already used START_STICKY). We also enabled Hermes engine in application.

Issue comes when Android OS kill our app due to any/random reason (Due to Doze mode), Application instance and Foreground service gets re-created as it is declared as START_STICKY. But React-native JS engine does not get re-created. We have some classes and manager declared in React-native which are not getting created due to this.

2022-04-13, 11:44:54 a.m. | INFO | ["DataConsumer -> Received hr data ",{"hr":76,"error":null,"mean":76,"std":2.7460647515915926,"snr":1.9386964917651681,"R2RAmp":11624.456661888753,"channel":1,"quality":100,"computedHR":0,"timeTaken":134,"startTime":1649864694235,"isRHR":true,"save":true,"leadState":81}]
2022-04-13, 11:44:54 a.m. | INFO | ["DataConsumer -> Saving hr data ",{"hr":76,"error":null,"mean":76,"std":2.7460647515915926,"snr":1.9386964917651681,"R2RAmp":11624.456661888753,"channel":1,"quality":100,"computedHR":0,"timeTaken":134,"startTime":1649864694235,"isRHR":true,"save":true,"leadState":81}]
2022-04-13, 11:44:57 a.m. | INFO | ["DataConsumer: Saving the ECG array for channel 1 with length of :",296400]
2022-04-13, 11:44:59 a.m. | DEBUG | ["DataValidationService: Acc Counter: 2822571"]
2022-04-13, 11:45:01 a.m. | INFO | ["DataConsumer: Saving the ECG array for channel 2 with length of :",296400]
2022-04-13, 11:45:02 a.m. | DEBUG | ["DataValidationService: Temperature 3692. Timestamp: 1649864700"]
2022-04-13, 11:45:03 a.m. | INFO | ["DataConsumer: Saving the ECG array for channel 3 with length of :",296400]
2022-04-13, 11:45:13 a.m. | DEBUG | ["DataValidationService: Ecg Ch: 1, Lead state 1. Counter: 2837265"]
2022-04-13, 11:45:13 a.m. | DEBUG | ["DataValidationService: Ecg Ch: 2, Lead state 1. Counter: 2837265"]
2022-04-13, 11:45:13 a.m. | DEBUG | ["DataValidationService: Ecg Ch: 3, Lead state 1. Counter: 2837265"]
Wed Apr 13 11:45:17 EDT 2022 ForegroundService onCreate called
Wed Apr 13 11:45:18 EDT 2022 ForegroundService onStartCommand
2022-04-13, 12:13:07 p.m. | INFO | ["App: Creating UI"]
2022-04-13, 12:13:07 p.m. | INFO | ["GlobalManager: Starting Managers ... ","2022-4-13 12:13:7"]
2022-04-13, 12:13:07 p.m. | INFO | ["GlobalManager: The build is debug? ",false]
2022-04-13, 12:13:07 p.m. | DEBUG | ["Is Internet connected?",{"details":{"isConnectionExpensive":false},"isInternetReachable":true,"isConnected":true,"type":"wifi"}]
2022-04-13, 12:13:07 p.m. | DEBUG | ["RemoteConfig -> Current environment swithched to","prod"]
2022-04-13, 12:13:07 p.m. | DEBUG | ["App is in foreground",null]
2022-04-13, 12:13:07 p.m. | INFO | ["GlobalManager: DB Initialized"]
2022-04-13, 12:13:07 p.m. | DEBUG | ["Auth manager -> The environment on initial Auth check is ","prod"]

Any idea about it?

mathias5r commented 2 years ago

If you don't have a high priority notification related to your app in the notification center, android system is going to kill you service to free memory and resources. Make sure this notification is shown all the time.

akashapps commented 2 years ago

@mathias5r I already have foreground service in Application. Although, When application gets kill, foreground service getting re-created because of flag "START_STICKY". But JS engine can not be re-created. JS engine only start when I click on icon and open app again (when App.js) getting called.

@mathias5r I have updated sample log in first message.

mathias5r commented 2 years ago

As far as I can remember, the HeadlessJS bundles just the necessary logic when it is created. i. e., when your service is triggered it's going to run just that piece of logic and not your whole app.