当应用程序关闭时,使用WorkManager不工作/不调度作业

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

当我们使用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]
}

}

java android android-workmanager
2个回答
0
投票

你的工人代码什么都不做。您正在返回null ListenableFuture,这意味着WorkManager无法执行任何操作。 onStartWork也明确标记为@NonNull所以我不确定你在那里做什么。你有一个永远不会执行的方法(toast)因为你不在任何地方调用它。


0
投票

我已经解决了问题,问题的答案就是让一个工作者扩展类,不要使用任何Toast或其他消息然后它将正常工作。

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