首先:这个问题和我上一个问题非常相似,但是答案不能帮助我……
我正在制作一个待办事项应用程序。我在推送通知上卡住了(即使应用程序没有运行)。我的目标是推送关于待办任务的通知,即使应用程序没有运行。
例如,我在x/y/2021上做了一个新的待办任务,时间是mm:hh。应用程序必须在该时刻推送通知,即使应用程序未运行
调度(delay as 10只是为了调试):
OneTimeWorkRequest notificationWorker = new OneTimeWorkRequest.Builder(NotificationWorker.class)
.setInitialDelay(10, TimeUnit.SECONDS)
.setInputData(new Data.Builder().putString("title", "Note").build()).build();
WorkManager workManager = WorkManager.getInstance(this);
workManager.enqueue(notificationWorker);类扩展Worker:
@NonNull
@Override
public Result doWork() {
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
String date = getInputData().getString("date");
String time = getInputData().getString("time");
String title = getInputData().getString("title");
String description = getInputData().getString("description");
int delay = getInputData().getInt("delay", 0);
Notification notification = new NotificationCompat.Builder(getApplicationContext(), "note")
.setSmallIcon(R.mipmap.logo)
.setContentTitle(title)
.setContentInfo(date + " at " + time)
.setContentText(description)
.build();
notificationManager.notify(1, notification);
return Result.success();
}发布于 2021-03-01 00:20:45
老实说,我不会读你的代码,但可以看看workManager(因为它就是为这些东西设计的):
安卓workManager文档:
WorkManager是一种应用程序编程接口,它可以轻松地调度可推迟的异步任务,即使应用程序退出或设备重新启动,这些任务也会运行。Android是所有以前的WorkManager后台调度API的合适和推荐的替代品,包括FirebaseJobDispatcher,GcmNetworkManager和Job Scheduler。WorkManager结合了其前身的功能,在一个现代的,一致的应用程序接口,工作回到API级别14,同时也意识到电池寿命。
workManager:https://developer.android.com/topic/libraries/architecture/workmanager
示例(我想这就是你想要的):https://medium.com/@ifr0z/workmanager-notification-date-and-time-pickers-aad1d938b0a3
https://stackoverflow.com/questions/66410931
复制相似问题