现代和正确的方式在Android应用程序中安排通知

huangapple go评论56阅读模式
英文:

Modern and correct way to schedule notification in an Android application

问题

所以我已经按照这个指南在我的Android应用程序中实现了通知,并在一些尝试之后使其正常工作。现在,我想要实现的是,在用户开始了一个活动任务时,每小时安排一次通知。通知将用于向用户更新他们的任务进度。

在一些研究后,我发现了一些关于如何使用AlarmManager安排通知的旧指南。然而,Stack Overflow 和 Android 论坛上的另一篇帖子说使用AlarmManager是不好的做法。

你们有没有人知道如何正确地安排一个或多个通知?另外,当任务完成时取消通知的能力也是必要的。

在应用程序中没有涉及后端,因此通知应该在设备上安排。

亲切的问候。

英文:

So I have implemented notifications in my Android application using this guide and after some attempts, I got it working. Now, what I want to achieve is to schedule notifications every hour when the user has an active quest started. The notification will be there to update the user about their quest.

After some research, I found some old guides on how to schedule the notifications using AlarmManager. However, another post on SO and the Android forum said that using AlarmManager is bad practice.

Do any of you know how to properly schedule one or more notifications? Also, the ability to cancel the notifications when the quest is finished is necessary.

There is no backend involved in the app, so the notification should be scheduled on the device itself.

Kind regards.

答案1

得分: 1

AlarmManager工作正常,但它是一个较旧、功能较少的API。它还不会在重启时保存状态。较新的API包括JobScheduler和WorkManager。第一个用于在特定时间通知或在条件发生时(如获得网络连接)通知。它适用于一次性或重复性的提醒。WorkManager用于后台工作,比如低优先级的数据库同步,在设备有空闲容量时执行。根据您的用例选择合适的API。如果您只是使用了一个闹钟,那可能是JobScheduler。

英文:

AlarmManager works, but it's an older, less capable API. It also doesn't save state around reboot. Newer APIs include JobScheduler and WorkManager. The first is used to be notified at a specific time and/or when conditions occur (such as gaining network connectivity). It's meant for 1 time or repeated alarms. WorkManager is for background work like a database sync that is low priority and meant to be done when the device has spare capacity. Use whichever is appropriate for your usecase. If you were just using an alarm, it's probably JobScheduler.

答案2

得分: 1

现代的工作调度与通知方式是WorkManager,WorkManager是持久性工作的推荐解决方案,您不需要使用服务或任何线程机制来完成任务,您还可以将其仅用作通知调度程序。

WorkManager还在后台使用AlaramManager,但这是处理作业、线程、通知等的更有用和高效的方式。

英文:

The modern way of scheduling jobs with notifications is WorkManager, WorkManager is the recommended solution for persistent work, you don't need to use a service or any threading mechanism to get tasks done, Also you can use it just as a Notification scheduler as well.

WorkManager also uses AlaramManager behind the scenes, but it's a more useful and efficient way to handle jobs, threads, notifications, etc

huangapple
  • 本文由 发表于 2023年6月16日 13:41:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定