我正在测试一个接收Firebase云数据消息并在Firebase作业服务中处理它们的应用程序。 FirebaseMessagingService中的接收消息立即发生,没有问题,但是Firebase作业服务有时会启动很长的延迟(5-10分钟),有时甚至根本无法启动。 dispatcher.schedule(myJob)方法始终给出结果SCHEDULE_RESULT_SUCCESS。这是作业调度功能:
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
Bundle bundle = new Bundle();
bundle.putInt(WatchDogService.REQUEST_ID, request.ordinal());
bundle.putString(REQUEST_PARAM, parameter);
FirebaseJobDispatcher dispatcher =
new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job myJob = dispatcher.newJobBuilder()
.setService(LongJobService.class)
.setTag("ua.ers.LongJobService")
.setTrigger(Trigger.NOW)
.setReplaceCurrent(true)
.setExtras(bundle)
.build();
int result = dispatcher.schedule(myJob);
Log.d(TAG, "Schedule result: " + result);
这是Firebase Job Service类:
public class LongJobService extends JobService {
private static final String TAG = "LongJobService";
@Override
public boolean onStartJob(final JobParameters jobParameters) {
Log.d(TAG, "LongJobService started");
return true;
}
@Override
public boolean onStopJob(JobParameters jobParameters) {
Log.d(TAG, "LongJobService stopped");
return false;
}
}
请告诉我,可能是什么原因造成的?
答案 0 :(得分:1)
根据文档,这只是通常的行为
鼓励调度程序后端使用windowEnd值作为应运行作业的信号,但这不是强制行为。
但是要获得更好的执行效果,请尝试
Trigger.executionWindow(0, 0)
代替
Trigger.NOW
所以看起来像
Job myJob = dispatcher.newJobBuilder()
.setService(Service.class)
.setRecurring(true) // if task is periodic, else pass "false"
.setTrigger(Trigger.executionWindow(0, 0))
.setTag("tag")
.build();