应用程序关闭时无法使用WorkManager进行工作/无法计划作业

时间:2019-04-08 20:19:04

标签: java android android-workmanager

由于我们在接收到Firebase通知时使用Firebase作业调度程序来运行作业,因此没有其他方法可以在接收到的旧通知上运行代码,现在是WorkManager。 在打开应用程序时它工作正常,但是在关闭应用程序时它不工作,但是firebase作业分派器工作正常,我希望它可以使用WorkManager API正常工作。

我以以下方式尝试了它是100%正常工作的代码,并使用WorkManager进行工作,但是只有在打开应用程序时,我希望它在未打开应用程序时才能工作,我知道我们已经为清单中Firebase作业调度程序的情况,但对WorkManager-:)

我尝试了与工作分配器相关的方法,但是它们不起作用,例如清单等中的服务...

公共类BackgroundWorker扩展了ListenableWorker {

private static final ListenableFuture listenableFuture = null ;

/**
 * @param appContext   The application {@link Context}
 * @param workerParams Parameters to setup the internal state of this worker
 */

public BackgroundWorker(@NonNull Context appContext, @NonNull WorkerParameters workerParams) {
    super(appContext, workerParams);
}

@NonNull
@Override
public ListenableFuture<ListenableWorker.Result> startWork() {
    // Do your work here.

    // Return a ListenableFuture<>
    return listenableFuture;
}

@Override
public void onStopped() {
    // Cleanup because you are being stopped.
}

public void toast(String msg, Context applicationContext)
{
    Toast.makeText(applicationContext,msg,Toast.LENGTH_LONG).show();
}

}

公共类MyFirebaseMessagingService扩展了FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sendNotification(remoteMessage.getData().get("body"),remoteMessage.getData().get("title"));


    scheduleJob();
}
/**
 * Called if InstanceID token is updated. This may occur if the security of
 * the previous token had been compromised. Note that this is called when the InstanceID token
 * is initially generated so this is where you would retrieve the token.
 */

/**
 * Schedule a job using workmanager.
 */

private void scheduleJob() {
    String unique_id = getRandomString(6);
    Data inputData = new Data.Builder()
            .putString("bulksmswebapi", unique_id)
            .build();
    // [START dispatch_job]

    Constraints constraints = new Constraints.Builder()
            // The Worker needs Network connectivity
            .setRequiredNetworkType(NetworkType.CONNECTED)
            // Needs the device to be charging
           // .setRequiresCharging(true)
            .build();
    OneTimeWorkRequest workRequest =
            // Tell which work to execute
            new OneTimeWorkRequest.Builder(BackgroundWorker.class)
                    // Sets the input data for the ListenableWorker
                    .setInputData(inputData)
                    // If you want to delay the start of work by 60 seconds
                    .setInitialDelay(1, TimeUnit.SECONDS)
                    // Set a backoff criteria to be used when retry-ing
                  //  .setBackoffCriteria(BackoffCriteria.EXPONENTIAL, 30000, TimeUnit.MILLISECONDS)
                    // Set additional constraints
                    .setConstraints(constraints)
                    .build();
    WorkManager.getInstance()
            // Use ExistingWorkPolicy.REPLACE to cancel and delete any existing pending
            // (uncompleted) work with the same unique name. Then, insert the newly-specified
            // work.
            .enqueueUniqueWork(unique_id, ExistingWorkPolicy.KEEP, workRequest);
    // [END dispatch_job]
}

}

2 个答案:

答案 0 :(得分:0)

您的工作程序代码不执行任何操作。您将返回null的ListenableFuture,这意味着WorkManager无法执行任何操作。 onStartWork也被明确标记为@NonNull,因此我不确定您在做什么。您有一个永远不会执行的方法(吐司),因为您没有在任何地方调用它。

答案 1 :(得分:0)

问题已由我解决,问题的答案是仅制作一个Worker扩展类,并且不使用任何Toast或其他消息,那么它将正常工作。