在Android应用程序中实现日历提醒通知
我正在构建一个类似 Android 应用程序的日历,我想添加一些将被触发的提醒通知,例如,在事件发生前 1 小时。因此,必须在不同时间安排多个通知。
我阅读了一些文档,发现我可以使用 AlarmManager 来做到这一点,但它似乎不太易于维护。这是正确的方法还是我需要使用其他方法?
如何在我的应用程序架构中添加通知,我是否为此创建服务?我在哪里创建通知通道(我会在 mainActivity
onCreate
函数上创建,但对我来说似乎不干净),此外,通道是否仍然存在如果应用程序关闭了?如果没有,要触发通知,我们需要唤醒应用程序并重新创建通道?
如果您有一个正在实现类似功能的示例应用程序,我会很高兴看到它。
I'm building a calendar like Android application and I want to add some reminder notifications that would be triggered, for exemple, 1 hour before an event. So multiple notifications would have to be scheduled at different time.
I read some documentation and I see that I could use AlarmManager to do that but it doesn't seems very maintainable. Is it the right way to do it or I need to use something else?
How do I add the notification in my app architecture, Do I create a Service for that? Where do I create the notification channel (I would do it on the mainActivity
onCreate
function, but it doesn't seems clean to me), furthermore, is the channel still existing if the app is closed? If not, to trigger a notification we'ld need to wake up the app and recreate a channel?
If you have am example app who's implementing something similar I'ld be glad to see it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我用来实现此目的的代码。我不确定这是否回答了您的问题
此解决方案首先在用户的日历中创建一个事件,然后在事件应该发生之前的一些(指定)分钟通过通知通知用户。
要将事件添加到用户的日历,您可以指定开始日期和时间、结束日期和时间、事件标题以及有关事件的任何其他信息(带有注释)。
要向事件添加提醒,需要使用 EventID 来标识您要添加提醒的确切事件。 eventID 包含在将事件添加到用户日历后返回的 uri 中。
添加提醒时,您可以指定您希望在事件发生前多少分钟通知用户。
在此代码片段中,notifyTime 用于指定您要在事件发生前多少分钟通知用户
希望这有帮助
Here's the code I used to achieve this. I'm not sure if this answers your question
This solution first creates an event within a user's calendar and then notifies the user with a notification some (specified) minutes before the event is supposed to take place.
For adding an event to a user's calendar you can specify the start date and time, end date and time, title of the event and any additional information about the event with a note.
To add a reminder to an event, an EventID is required to identify the exact event you're adding the reminder to. The eventID is contained in the uri that is returned after an event has been added to the user's calendar.
When adding a reminder, you can specify how many minutes before the event you would like the user to be notified.
In this snippet, notifyTime is used to specify how many minutes before the event you would like to notify the user
Hope this helps