0

I have created a React native application, which has a foreground service that needs to be started automatically when the device is rebooted. This foreground service functions 100% when it is started normally when opening the app (as I've configured it to do). For context, the service is created in a native module which I have developed, and that native module is imported in the React native application and that's how the service is invoked.

I now am trying to solve for the issue of invoking the native module and getting the service to start without the user needing to open the app. I'm running into an issue where it seems that my app is constantly being killed while it is restarting on a reboot.

I have noticed that whenever I reboot the device with the application installed (in debug mode), there is activity in the logcat in Android studio.

It seems that the device is trying to start the application, but for some reason it is always getting killed. I noticed this while using the react-native-background-fetch package in my application - where I have created a task to run on boot (I have followed the instructions to do this and I believe my implementation is correct as far as I can tell).

These are the logs I'm seeing in Android studio - with the kill log at the end

---------------------------- PROCESS STARTED (8734) for package com.btech.app ----------------------------
...
...
2023-01-26 14:33:36.067  8734-8734  com.btech.app           com.btech.app                        W  Accessing hidden field Landroid/view/View;->mKeyedTags:Landroid/util/SparseArray; (unsupported, reflection, allowed)
2023-01-26 14:33:36.067  8734-8734  com.btech.app           com.btech.app                        W  Accessing hidden field Landroid/view/View;->mListenerInfo:Landroid/view/View$ListenerInfo; (unsupported, reflection, allowed)
2023-01-26 14:33:36.068  8734-8734  com.btech.app           com.btech.app                        W  Accessing hidden field Landroid/view/View$ListenerInfo;->mOnClickListener:Landroid/view/View$OnClickListener; (unsupported, reflection, allowed)
2023-01-26 14:33:36.085  8734-8734  flipper                 com.btech.app                        I  flipper: FlipperClient::addPlugin Inspector
2023-01-26 14:33:36.087  8734-8734  flipper                 com.btech.app                        I  flipper: FlipperClient::addPlugin React
2023-01-26 14:33:36.094  8734-8734  flipper                 com.btech.app                        I  flipper: FlipperClient::addPlugin Databases
2023-01-26 14:33:36.105  8734-8734  flipper                 com.btech.app                        I  flipper: FlipperClient::addPlugin Preferences
2023-01-26 14:33:36.106  8734-8734  flipper                 com.btech.app                        I  flipper: FlipperClient::addPlugin CrashReporter
2023-01-26 14:33:36.114  8734-8734  flipper                 com.btech.app                        I  flipper: FlipperClient::addPlugin Network
2023-01-26 14:33:36.123  8734-8734  System.out              com.btech.app                        I  BOOT RECEIVER INITIALISED - sending to RN 
2023-01-26 14:33:36.131  8734-8734  Choreographer           com.btech.app                        I  Skipped 62 frames!  The application may be doing too much work on its main thread.
2023-01-26 14:33:36.135  8734-8734  RebootReceiver          com.btech.app                        I  Received reboot event
2023-01-26 14:33:36.138  8734-8734  NOTIFEE                 com.btech.app                        D  (NotifeeAlarmManager): Reschedule Notifications on reboot
2023-01-26 14:33:36.167  8734-8809  com.btech.app           com.btech.app                        W  Accessing hidden method Landroid/app/AppGlobals;->getInitialApplication()Landroid/app/Application; (unsupported, reflection, allowed)
2023-01-26 14:33:36.176  8734-8734  TSBackgroundFetch       com.btech.app                        D  BootReceiver: android.intent.action.BOOT_COMPLETED
2023-01-26 14:33:36.186  8734-8734  TSBackgroundFetch       com.btech.app                        D  ☯️  onCreate
2023-01-26 14:33:36.237  8734-8734  TSBackgroundFetch       com.btech.app                        D  ☯️  HeadlessMode? true
2023-01-26 14:33:38.983  8734-8752  System                  com.btech.app                        W  A resource failed to call close. 
2023-01-26 14:33:41.991  8734-9389  ProfileInstaller        com.btech.app                        D  Installing profile for com.btech.app
2023-01-26 14:33:46.947  1311-3881  ActivityManager         pid-1311                             I  Killing 8734:com.btech.app/u0a263 (adj 999): empty #22

I'm not quite sure what can be done about the warnings (The 'A resource failed to call close' and the 'Accessing hidden method/fields' messages in the logs) but I'm not actually sure if these are something that would be killing the app and would need to be addressed.

What I've noticed is that, even without the background fetch task configured and the package completely removed, it is still showing the same logs and trying to restart the application, just without the TSBackgroundFetch library's logs in the logcat. I've tried various settings on configuring that job but they have all had the same result.

The only thing that I have noticed that is consistent, is that the app is ALWAYS killed after the second last line in the logs, which says 'Installing profile for com.btech.app' and I'm not exactly sure of what this line actually means or if it could be the point at which the restart is failing.

For reference, here's the job as I have it configured currently. When I open the app, I can see that the job is configured and from the logs it is evident that it does actually try to execute the code but doesn't get to in the end. This code is in my app's index.js file.

async function initialiseBackgroundRebootTaskForAndroid() {
  logger.info("Starting android reboot task registration...")
  if (Platform.OS === 'android') {
    const onRunJob = async (taskID) => {
      logger.info("Running reboot task.")
      initialiseApp()
      BackgroundFetch.finish(taskID);
    }

    await BackgroundFetch.configure({
      minimumFetchInterval: 15,
      stopOnTerminate: false,
      startOnBoot: true,
      enableHeadless: false,
    }, onRunJob, (error) => {
      logger.info("RNBackgroundFetch failed to start");
    }).then(() => {
      logger.info('[RNBackgroundReinitAppJob] Background restart job scheduled.')
    })

  } else {
    logger.info("Task not started - device is iOS...")

  }
}

Additionally, I have also tried to create my own native BOOT_COMPLETED receiver on the Android side, and in the onReceive listener, broadcast an event to a LocalBroadcastManager listener on the React Native side, but I think that receiver gets killed when the device switches off so that actually isn't registered any longer when the app receives the BOOT_COMPLETED intent. In the logs you can see that the intent was received but I don't know how to see if the message is received in the JS side because my device is no longer connected to metro after a reboot.

I need to fix this so that the app is not killed automatically all the time. Does anyone know what could be causing this?

Thanks!

Marko Vidalis
  • 331
  • 1
  • 7
  • 18

0 Answers0