从 GCM 迁移到 FCM 后的 PeriodicTask 和 OneoffTask
PeriodicTask and OneoffTask after migration from GCM to FCM
问题
从 GCM 迁移到 FCM 时,我应该使用什么来代替 PeriodicTask 和 OneOffTask? FCM 包中是否有任何等效项?
背景
最近 Google 弃用了 GCM,他们提供了 migration guide,但我在那里找不到任何关于迁移 PeriodicTasks 和 OneOffTasks 的信息。
教程说到replace play-services-gcm
with firebase-messaging
but I couldn't find anything similar to the tasks inside this package,所以我想知道他们是否完全放弃了他们,或者我们是否真的可以继续使用PeriodicTask/OneOffTasks?
PeriodicTask 和 OneoffTask 类(GcmNetworkManager 组件的一部分)可以替换为 Firebase Job Dispatcher,它是一个开源库,用于调度将在您的应用程序中执行的工作单元过程。它公开的服务端 API 镜像框架的 JobScheduler API,与 GcmNetworkManager 略有不同。升级应该是一个相当简单的机械变化。
升级步骤
将以下内容添加到 build.gradle
的依赖项部分:
implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
让您的 GcmTaskService
实现扩展 JobService
。来自:
public class MyTaskService extends GcmTaskService {
@Override
public int onRunTask(TaskParams params) {
// Runs in its own thread
// ...
return RESULT_SUCCESS;
}
}
为此:
public class MyJobService extends JobService {
@Override
public boolean onStartJob(JobParameters job) {
// Runs on the main thread!
// ...
return false; // "Did we start any background work?"
}
@Override
public boolean onStopJob(JobParameters job) {
// Runs on the main thread!
// ...
return false; // "Should this job be retried?"
}
}
如果不想自己管理线程,可以利用库自带的SimpleJobService
:
public class MyJobService extends SimpleJobService {
@Override
public int onRunJob(JobParameters job) {
// Runs on its own thread!
// ...
return JobService.RESULT_SUCCESS;
}
}
确保调整清单。将您之前的 GcmTaskService
条目替换为如下内容:
<service
android:exported="false"
android:name=".MyJobService">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
调度很简单:
// Create a new dispatcher using the Google Play driver.
FirebaseJobDispatcher dispatcher =
new FirebaseJobDispatcher(new GooglePlayDriver(context));
Job myJob = dispatcher.newJobBuilder()
// the JobService that will be called
.setService(MyJobService.class)
// uniquely identifies the job
.setTag("my-unique-tag")
// one-off job (only runs once unless a retry is requested)
.setRecurring(false)
// don't persist past a device reboot (requires RECEIVE_BOOT_COMPLETE)
.setLifetime(Lifetime.UNTIL_NEXT_BOOT)
// start between 0 and 60 seconds from now
.setTrigger(Trigger.executionWindow(0, 60))
// don't overwrite an existing job with the same tag
.setReplaceCurrent(false)
// retry with exponential backoff
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
// constraints that need to be satisfied for the job to run
.setConstraints(
// only run on an unmetered network
Constraint.ON_UNMETERED_NETWORK,
// only run when the device is charging
Constraint.DEVICE_CHARGING
)
.setExtras(myExtrasBundle)
.build();
dispatcher.mustSchedule(myJob);
查看 repo 了解更多信息。
问题
从 GCM 迁移到 FCM 时,我应该使用什么来代替 PeriodicTask 和 OneOffTask? FCM 包中是否有任何等效项?
背景
最近 Google 弃用了 GCM,他们提供了 migration guide,但我在那里找不到任何关于迁移 PeriodicTasks 和 OneOffTasks 的信息。
教程说到replace play-services-gcm
with firebase-messaging
but I couldn't find anything similar to the tasks inside this package,所以我想知道他们是否完全放弃了他们,或者我们是否真的可以继续使用PeriodicTask/OneOffTasks?
PeriodicTask 和 OneoffTask 类(GcmNetworkManager 组件的一部分)可以替换为 Firebase Job Dispatcher,它是一个开源库,用于调度将在您的应用程序中执行的工作单元过程。它公开的服务端 API 镜像框架的 JobScheduler API,与 GcmNetworkManager 略有不同。升级应该是一个相当简单的机械变化。
升级步骤
将以下内容添加到 build.gradle
的依赖项部分:
implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
让您的 GcmTaskService
实现扩展 JobService
。来自:
public class MyTaskService extends GcmTaskService {
@Override
public int onRunTask(TaskParams params) {
// Runs in its own thread
// ...
return RESULT_SUCCESS;
}
}
为此:
public class MyJobService extends JobService {
@Override
public boolean onStartJob(JobParameters job) {
// Runs on the main thread!
// ...
return false; // "Did we start any background work?"
}
@Override
public boolean onStopJob(JobParameters job) {
// Runs on the main thread!
// ...
return false; // "Should this job be retried?"
}
}
如果不想自己管理线程,可以利用库自带的SimpleJobService
:
public class MyJobService extends SimpleJobService {
@Override
public int onRunJob(JobParameters job) {
// Runs on its own thread!
// ...
return JobService.RESULT_SUCCESS;
}
}
确保调整清单。将您之前的 GcmTaskService
条目替换为如下内容:
<service
android:exported="false"
android:name=".MyJobService">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
调度很简单:
// Create a new dispatcher using the Google Play driver.
FirebaseJobDispatcher dispatcher =
new FirebaseJobDispatcher(new GooglePlayDriver(context));
Job myJob = dispatcher.newJobBuilder()
// the JobService that will be called
.setService(MyJobService.class)
// uniquely identifies the job
.setTag("my-unique-tag")
// one-off job (only runs once unless a retry is requested)
.setRecurring(false)
// don't persist past a device reboot (requires RECEIVE_BOOT_COMPLETE)
.setLifetime(Lifetime.UNTIL_NEXT_BOOT)
// start between 0 and 60 seconds from now
.setTrigger(Trigger.executionWindow(0, 60))
// don't overwrite an existing job with the same tag
.setReplaceCurrent(false)
// retry with exponential backoff
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
// constraints that need to be satisfied for the job to run
.setConstraints(
// only run on an unmetered network
Constraint.ON_UNMETERED_NETWORK,
// only run when the device is charging
Constraint.DEVICE_CHARGING
)
.setExtras(myExtrasBundle)
.build();
dispatcher.mustSchedule(myJob);
查看 repo 了解更多信息。