Android.app.remoteserviceexception: Context.startforegroundservice() Did Not Then Call Service.startforeground( ) In React Native
I'm trying to create background service in Android Oreo using react-native-background-job but when I'll start the service is call success but Showing crash dialog service is stoppe
Solution 1:
You are not allowed to start background service using startForegroundService
- if service doesn't elevate itself into the foreground with startForeground
and notification within 5 seconds, it will be killed by the system.
Take a look at related question and answers.
Solution 2:
I got the solution in for react-native-background-job
android/src/main/java/com/pilloxa/backgroundjob/ReactNativeEventStarter.java
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
public staticclassMyHeadlessJsTaskServiceextendsHeadlessJsTaskService {
private static final StringLOG_TAG = MyHeadlessJsTaskService.class.getSimpleName();
@Override
@SuppressLint("WrongConstant")
public voidonCreate() {
super.onCreate();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Context mContext = this.getApplicationContext();
StringCHANNEL_ID = "Background job";
NotificationChannel channel = newNotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
Notification notification =
newNotification.Builder(mContext, CHANNEL_ID)
.setContentTitle("Running background job")
.setContentText(mContext.getPackageName())
.setSmallIcon(R.drawable.ic_notification)
.build();
startForeground(1, notification);
}
}
@Nullable @Override protected HeadlessJsTaskConfiggetTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();
boolean allowExecutionInForeground = extras.getBoolean("allowExecutionInForeground", false);
long timeout = extras.getLong("timeout", 2000);
// For task with quick execution period additional check is requiredReactNativeHost reactNativeHost =
((ReactApplication) getApplicationContext()).getReactNativeHost();
boolean appInForeground = Utils.isReactNativeAppInForeground(reactNativeHost);
if (appInForeground && !allowExecutionInForeground) {
returnnull;
}
returnnewHeadlessJsTaskConfig(intent.getStringExtra("jobKey"), Arguments.fromBundle(extras),
timeout, allowExecutionInForeground);
}
public staticvoidstart(Context context, Bundle jobBundle) {
Intent starter = newIntent(context, MyHeadlessJsTaskService.class);
starter.putExtras(jobBundle);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(starter);
}else{
context.startService(starter);
}
}
}
}
Post a Comment for "Android.app.remoteserviceexception: Context.startforegroundservice() Did Not Then Call Service.startforeground( ) In React Native"