不是 Working/Not 在应用程序关闭时使用 WorkManager 安排作业
Not Working/Not Scheduling Job using WorkManager When App is Closed
当我们在收到 firebase 通知时使用 firebase 作业调度程序 运行 作业,没有其他方法可以 运行 接收到的旧通知上的代码,现在 WorkManager 在这里。
当应用程序打开时它工作正常但当应用程序关闭时它不起作用,但是 firebase 作业调度器工作正常,我希望它使用 WorkManager API.
我尝试了以下方式,它是 100% 工作代码并使用 WorkManager 完成工作,但只有当应用程序打开时,我希望它在应用程序未打开时工作,我知道我们注册了一个服务对于清单中的 firebase 作业调度程序,但如何为 WorkManager -:)
我尝试了与作业调度程序相关的方法,但它们没有用,例如清单中的服务等...
public class 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();
}
}
public class 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]
}
}
你的工人代码什么都不做。您正在返回 null ListenableFuture,这意味着 WorkManager 无法执行任何操作。 onStartWork 也被明确标记为 @NonNull 所以我不确定你在那里做什么。您有一个从未执行过的方法 (toast),因为您没有在任何地方调用它。
问题由我解决,问题的答案是扩展一个 Worker class 并且不要使用任何 Toast 或其他消息,然后它会正常工作。
当我们在收到 firebase 通知时使用 firebase 作业调度程序 运行 作业,没有其他方法可以 运行 接收到的旧通知上的代码,现在 WorkManager 在这里。 当应用程序打开时它工作正常但当应用程序关闭时它不起作用,但是 firebase 作业调度器工作正常,我希望它使用 WorkManager API.
我尝试了以下方式,它是 100% 工作代码并使用 WorkManager 完成工作,但只有当应用程序打开时,我希望它在应用程序未打开时工作,我知道我们注册了一个服务对于清单中的 firebase 作业调度程序,但如何为 WorkManager -:)
我尝试了与作业调度程序相关的方法,但它们没有用,例如清单中的服务等...
public class 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();
}
}
public class 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]
}
}
你的工人代码什么都不做。您正在返回 null ListenableFuture,这意味着 WorkManager 无法执行任何操作。 onStartWork 也被明确标记为 @NonNull 所以我不确定你在那里做什么。您有一个从未执行过的方法 (toast),因为您没有在任何地方调用它。
问题由我解决,问题的答案是扩展一个 Worker class 并且不要使用任何 Toast 或其他消息,然后它会正常工作。