Running android service from android worker when android application closed and got broadcast message

Gruzchick

New member
Aug 7, 2024
1
0
1
Visit site
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

Java:
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);
  }
}
Java:
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 ();
  }
}
Java:
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?
 

Laura Knotek

Moderator Captain
Moderator
Jan 8, 2011
12,307
5,517
113
Visit site
We don't really have many people here at Android Central that do software development like this. My suggestion is for you to post at XDA Developers since they are more involved in that stuff. Good luck.
 
  • Like
Reactions: mustang7757

Forum statistics

Threads
947,470
Messages
6,934,001
Members
3,160,794
Latest member
Robert-42