当Android应用程序关闭并收到广播消息时,如何从Android Worker运行Android服务?

问题描述 投票:0回答:1

我的react-native android应用程序可以从多个应用程序商店下载。

我需要做到这一点,以便某些推送通知仅发送到从某些应用程序商店下载的应用程序的实例。但由于从一个应用程序商店下载的应用程序可以从另一个应用程序商店更新,因此我需要通知服务器这一事实。

为此,我监听广播消息并运行我的代码。

这是我的代码

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
    );
  }
}

如您所见,我正在尝试运行 React-native Headless JS 任务,但是当应用程序关闭时,前台任务无法从 Worker 运行。

当我的应用程序关闭时,如何从 Worker 运行react-native headlessjs 任务?

android react-native
1个回答
0
投票

我有一个新方法:Android 变体。

buidl.gradle

android {
    defaultConfig {
        ...
        buildConfigField('String', 'type_google', '"googlePlayStore"')
        buildConfigField('String', 'type_web', '"myWeb"')
    }

    buildTypes {
        apptype1{
            ...
            buildConfigField('String', 'type', '"googlePlayStore"')
        }
        apptype2{
            ...
            buildConfigField('String', 'type', '"myWeb"')
        }
    }
}

比,您可以在应用程序打开时通知服务器。也不显示推送数据。

if(BuildConfig.type.equals(BuildConfig.type_google)){
    // TODO do some thing
}

准备好发布应用程序后,您可以选择构建apptype1apptype2并构建全部。

两个应用程序之间的唯一区别就是这个变量(

BuildConfig.type
)

© www.soinside.com 2019 - 2024. All rights reserved.