android.app.RemoteServiceException: Context.startForegroundService() 没有在 React native 中调用 Service.startForeground( )

android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground( ) in React native

我正在尝试使用 react-native-background-job 在 Android Oreo 中创建后台服务,但是当我启动该服务时调用成功但显示崩溃对话框服务已停止。请提供此问题的解决方案。

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

您不能使用 startForegroundService 启动后台服务 - 如果服务没有在 5 秒内通过 startForeground 和通知将自己提升到前台,它将被系统杀死。

看看related question and answers

我得到了 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);
      }
    }
  }
}