com.facebook.react.jstasks.HeadlessJsTaskConfig Java Examples

The following examples show how to use com.facebook.react.jstasks.HeadlessJsTaskConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HeadlessTaskService.java    From react-native-background-task with MIT License 6 votes vote down vote up
@Override
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
    Bundle extras = intent.getExtras();
    // If extras have been passed to the intent, pass them on into the JS as taskData
    // which can be accessed as the first param.
    WritableMap data = /* extras != null ? Arguments.fromBundle(extras) : */ Arguments.createMap();

    int timeout = extras.getInt("timeout");

    Log.d(TAG, String.format("Returning HeadlessJsTaskConfig, timeout=%s ms", timeout));
    return new HeadlessJsTaskConfig(
            // The the task was registered with in JS - must match
            "BackgroundTask",
            data,
            TimeUnit.SECONDS.toMillis(timeout)
    );
}
 
Example #2
Source File: HeadlessService.java    From react-native-sync-adapter with MIT License 6 votes vote down vote up
@Override
protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
    boolean allowForeground = Boolean.parseBoolean(getString(R.string.rnsb_allow_foreground));

    if(allowForeground || !isAppOnForeground(this)) {
        Bundle extras = intent.getExtras();
        WritableMap data = extras != null ? Arguments.fromBundle(extras) : Arguments.createMap();
        return new HeadlessJsTaskConfig(
                TASK_ID,
                data,
                Long.valueOf(getString(R.string.rnsb_default_timeout)),
                allowForeground);
    }

    stopSelf();
    return null;
}
 
Example #3
Source File: HeartbeatEventService.java    From rn-heartbeat with MIT License 5 votes vote down vote up
@Nullable
protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
    Bundle extras = intent.getExtras();
    return new HeadlessJsTaskConfig(
            "Heartbeat",
            extras != null ? Arguments.fromBundle(extras) : Arguments.createMap(),
            5000,
            true);
}
 
Example #4
Source File: RNCallKeepBackgroundMessagingService.java    From react-native-callkeep with ISC License 5 votes vote down vote up
@Override
protected @Nullable
HeadlessJsTaskConfig getTaskConfig(Intent intent) {
  Bundle extras = intent.getExtras();

  return new HeadlessJsTaskConfig(
    "RNCallKeepBackgroundMessage",
    Arguments.fromBundle(extras),
    60000,
    false
  );
}
 
Example #5
Source File: BoundaryEventHeadlessTaskService.java    From react-native-boundary with Apache License 2.0 5 votes vote down vote up
@Nullable
protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
    Bundle extras = intent.getExtras();
    return new HeadlessJsTaskConfig(
            "OnBoundaryEvent",
            extras != null ? Arguments.fromBundle(extras) : null,
            5000,
            true);
}
 
Example #6
Source File: HeadlessJsTaskService.java    From react-native-GPay with MIT License 5 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  HeadlessJsTaskConfig taskConfig = getTaskConfig(intent);
  if (taskConfig != null) {
    startTask(taskConfig);
    return START_REDELIVER_INTENT;
  }
  return START_NOT_STICKY;
}
 
Example #7
Source File: HeadlessJsTaskService.java    From react-native-GPay with MIT License 5 votes vote down vote up
private void invokeStartTask(ReactContext reactContext, final HeadlessJsTaskConfig taskConfig) {
  final HeadlessJsTaskContext headlessJsTaskContext = HeadlessJsTaskContext.getInstance(reactContext);
  headlessJsTaskContext.addTaskEventListener(this);

  UiThreadUtil.runOnUiThread(
    new Runnable() {
      @Override
      public void run() {
        int taskId = headlessJsTaskContext.startTask(taskConfig);
        mActiveTasks.add(taskId);
      }
    }
  );
}
 
Example #8
Source File: ReactNativeEventStarter.java    From react-native-background-job with MIT License 5 votes vote down vote up
@Nullable @Override protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
  Log.d(LOG_TAG, "getTaskConfig() called with: 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);
}
 
Example #9
Source File: HeadlessService.java    From react-native-background-geolocation with Apache License 2.0 5 votes vote down vote up
@Override
protected @Nullable
HeadlessJsTaskConfig getTaskConfig(Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
        return new HeadlessJsTaskConfig(
                TASK_KEY,
                Arguments.fromBundle(extras),
                60000, // timeout for the task
                true // optional: defines whether or not  the task is allowed in foreground. Default is false
        );
    }
    return null;
}
 
Example #10
Source File: HeadlessJsTaskService.java    From react-native-GPay with MIT License 2 votes vote down vote up
/**
 * Called from {@link #onStartCommand} to create a {@link HeadlessJsTaskConfig} for this intent.
 * @param intent the {@link Intent} received in {@link #onStartCommand}.
 * @return a {@link HeadlessJsTaskConfig} to be used with {@link #startTask}, or
 *         {@code null} to ignore this command.
 */
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
  return null;
}