1

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 stopped. please provide a solution for this problem.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(starter); } else { context.startService(starter); }

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 static class MyHeadlessJsTaskService extends HeadlessJsTaskService { private static final String LOG_TAG = MyHeadlessJsTaskService.class.getSimpleName(); @Override @SuppressLint("WrongConstant") public void onCreate() { super.onCreate(); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { Context mContext = this.getApplicationContext(); String CHANNEL_ID = "Background job"; NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW); ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel); Notification notification = new Notification.Builder(mContext, CHANNEL_ID) .setContentTitle("Running background job") .setContentText(mContext.getPackageName()) .setSmallIcon(R.drawable.ic_notification) .build(); startForeground(1, notification); } } @Nullable @Override protected HeadlessJsTaskConfig getTaskConfig(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 required ReactNativeHost reactNativeHost = ((ReactApplication) getApplicationContext()).getReactNativeHost(); boolean appInForeground = Utils.isReactNativeAppInForeground(reactNativeHost); if (appInForeground && !allowExecutionInForeground) { return null; } return new HeadlessJsTaskConfig(intent.getStringExtra("jobKey"), Arguments.fromBundle(extras), timeout, allowExecutionInForeground); } public static void start(Context context, Bundle jobBundle) { Intent starter = new Intent(context, MyHeadlessJsTaskService.class); starter.putExtras(jobBundle); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(starter); }else{ context.startService(starter); } } } }

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.