diff --git a/packages/react-native/android/src/main/java/io/invertase/notifee/HeadlessTask.java b/packages/react-native/android/src/main/java/io/invertase/notifee/HeadlessTask.java index eb5f6a59..24d3c9da 100644 --- a/packages/react-native/android/src/main/java/io/invertase/notifee/HeadlessTask.java +++ b/packages/react-native/android/src/main/java/io/invertase/notifee/HeadlessTask.java @@ -107,6 +107,7 @@ static synchronized int getNextTaskId() { private final List mTaskQueue = new ArrayList<>(); private final AtomicBoolean mIsReactContextInitialized = new AtomicBoolean(false); + private final AtomicBoolean mWillDrainTaskQueue = new AtomicBoolean(false); private final AtomicBoolean mIsInitializingReactContext = new AtomicBoolean(false); private final AtomicBoolean mIsHeadlessJsTaskListenerRegistered = new AtomicBoolean(false); @@ -184,6 +185,10 @@ public void startTask(Context context, final TaskConfig taskConfig) throws Asser private synchronized void invokeStartTask( ReactContext reactContext, final TaskConfig taskConfig) { + if (taskConfig.mReactTaskId > 0) { + Log.w(HEADLESS_TASK_NAME, "Task already invoked : " + this); + return; + } final HeadlessJsTaskContext headlessJsTaskContext = HeadlessJsTaskContext.getInstance(reactContext); try { @@ -339,16 +344,18 @@ public void onReactContextInitialized(@NonNull ReactContext reactContext) { * @param reactContext */ private void drainTaskQueue(final ReactContext reactContext) { - new Handler(Looper.getMainLooper()) - .postDelayed( - () -> { - synchronized (mTaskQueue) { - for (TaskConfig taskConfig : mTaskQueue) { - invokeStartTask(reactContext, taskConfig); + if (mWillDrainTaskQueue.compareAndSet(false, true)) { + new Handler(Looper.getMainLooper()) + .postDelayed( + () -> { + synchronized (mTaskQueue) { + for (TaskConfig taskConfig : mTaskQueue) { + invokeStartTask(reactContext, taskConfig); + } } - } - }, - 500); + }, + 500); + } } /**