How to run android service from android worker when android application closed and got broadcast message?

My react-native android app is available for download from multiple app stores.

I need to make it so that certain push notifications are sent only to instances of the app downloaded from certain app stores. But since an app downloaded from one app store can be updated from another app store, I need to notify the server of this fact.

To do this, I listen for the broadcast message and run my code.

Here is my code

package com.wbdigitalmobile;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;

public class PackageReplacedReceiver extends BroadcastReceiver {
  private static final String TAG = "PackageReplacedReceiver";

  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "onReceive");

    OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(PackageReplacedWorker.class).build();

    WorkManager.getInstance(context).enqueue(request);
  }
}
package com.wbdigitalmobile;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import com.facebook.react.HeadlessJsTaskService;

public class PackageReplacedWorker extends Worker {
  private static final String TAG = "PackageReplacedWorker";
  private final Context context;

  public PackageReplacedWorker (Context context, WorkerParameters workerParams ) {
    super ( context, workerParams );
    this.context = context;
  }

  @NonNull
  @Override
  public Result doWork () {
    Log.d(TAG, "doWork");

    Intent serviceIntent = new Intent(this.context, PushNotificationTokenUpdateService.class);

    this.context.startService(serviceIntent);
    HeadlessJsTaskService.acquireWakeLockNow(this.context);

    return Result.success ();
  }
}

package com.wbdigitalmobile;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;

import javax.annotation.Nullable;

public class PushNotificationTokenUpdateService extends HeadlessJsTaskService {
  private static final String TAG = "PushNotificationTokenUpdateService";
  public static final int SERVICE_ID = 800000;
  private static final String CHANNEL_ID = "PushNotificationTokenUpdateService";

  private Notification buildNotification() {
    final String taskTitle = "taskTitle";
    final String taskDesc = "taskDesc";

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
      .setContentTitle(taskTitle)
      .setContentText(taskDesc);

    return builder.build();
  }

  private void createNotificationChannel() {
    final int importance = NotificationManager.IMPORTANCE_LOW;

    final NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "taskTitle", importance);

    channel.setDescription("taskDesc");

    final NotificationManager notificationManager = getSystemService(NotificationManager.class);
    
    notificationManager.createNotificationChannel(channel);
  }

  @Override
  public void onCreate() {
    super.onCreate();

    createNotificationChannel();

    startForeground(SERVICE_ID, buildNotification());
  }

  @Override
  protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
    Log.d(TAG, "getTaskConfig");

    return new HeadlessJsTaskConfig(
      "PushNotificationTokenUpdateService",
      Arguments.createMap(),
      5000,
      true
    );
  }
}

As you can see, I am trying to run React-native Headless JS task, but the foreground task can’t be run from Worker when application closed.

How can I run react-native headlessjs task from Worker when my application is closed?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật