我要求根据以下逻辑将app通知推送给用户。
我使用PeriodicWorkRequest
工作管理器如下,它工作正常,直到设备重新启动。设备重启后,我的工作没有触发。
build.gradle ---
implementation 'android.arch.work:work-runtime:1.0.0-alpha04'
Java代码
PeriodicWorkRequest showNotification =
new PeriodicWorkRequest.Builder(ShowNotificationWorkManager.class, interval,
TimeUnit.HOURS)
.addTag(notificationType)
.setInputData(myData)
.build();
getWorkManger().enqueue(showNotification);
在设备完成启动时触发的BroadcastReceiver中重新启动PeriodicWorkRequest。像意图过滤器一样。
<receiver
android:name=".warrantyregistration.boot.BootReceiver"
android:enabled="@bool/configuration_for_receiver_service">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
this回答了重启设备时OneTimeWorkRequest的行为方式。
请在您的Android清单中添加以下权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />