【发布时间】:2018-12-14 06:02:44
【问题描述】:
想要安排在每周一上午 11 点触发的通知。 我正在为此使用firebase作业调度程序。 这是我已经实现的代码 sn-p 但这不起作用。
Calendar currentDate = Calendar.getInstance();
while (currentDate.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
currentDate.add(Calendar.DATE, 1);
}
currentDate.set(Calendar.HOUR_OF_DAY, 11);
currentDate.set(Calendar.MINUTE, 0);
currentDate.set(Calendar.SECOND, 0);
currentDate.set(Calendar.MILLISECOND, 0);
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(SplashScreen.this));
Job myJob = dispatcher.newJobBuilder()
.setService(ScheduledNotificationService.class)
.setTag(dispatcherTag)
.setRecurring(true)
.setLifetime(Lifetime.FOREVER)
.setTrigger(Trigger.executionWindow(Math.round(currentDate.getTime().getTime() / 1000), Math.round(currentDate.getTime().getTime() / 1000) + 60))
.setReplaceCurrent(false)
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
.build();
dispatcher.mustSchedule(myJob);
ScheduledNotificationService.class 扩展了 jobservice 但永远不会调用 onStartJob。
这里有什么问题?
除了使用 Firebase 作业调度程序之外,还有什么更好/正确的方法吗?
【问题讨论】:
-
我假设您的这段代码 sn-p 在 SplashScreen 或 MainActivity 中被调用?您是否在 Manifest 中注册了该服务。
-
它在 SplasScreen 中被调用,但我想 setReplaceCurent(false) 不会造成任何麻烦。是的,ScheduledNotificationService.class 已在 AndroidManifest 文件中注册。
-
您可以尝试将其测试为每 30 秒运行一次,而不是周一上午 11:00,看看它是否有效。当您的启动画面也打开时,检查您的 android 日志在 logcat 中。它说了什么。
-
我试过了,但服务没有被调用。我怀疑, .setTrigger(Trigger.executionWindow(Math.round(currentDate.getTime().getTime() / 1000), Math.round(currentDate.getTime().getTime() / 1000) + 60)) 在不知何故,不确定到底是什么。
标签: android android-jobscheduler firebase-job-dispatcher