Is there any way to trigger an event in react-native when the device is booted up. I can set a broadcast receiver in native for RECEIVE_BOOT_COMPLETED but I wanted to know how this will trigger my event written in React Native. There is a DeviceEventEmitter listener but that listener will stop when the app closes. Please suggest a way.
Once, I made something similar when I wanted to start the app alongside the Android OS initialization.
So, in this scenario, you can just start the main activity (or another one that make sense in your context) and let the react-native framework do the rest.
Three things are necessary to this works:
My code:
In my case, I put the created BootUpReceiver.java class at the same directory of MainActivity.java:

package com.your_package;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.your_package.MainActivity;
public class BootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent it = new Intent(context, MainActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
}
}
receiver tag inside your application tag: <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:enabled="true"
android:name=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Well, I hope this help, but if I this it's not enough, please take a time and check this link of React Native documentation which explains how to connect the an Android Service to a JS Task.
It's very easy: https://facebook.github.io/react-native/docs/headless-js-android
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With