在Android应用程序中实现日历提醒通知

发布于 2025-01-13 12:42:07 字数 361 浏览 3 评论 0原文

我正在构建一个类似 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风也等你 2025-01-20 12:42:07

这是我用来实现此目的的代码。我不确定这是否回答了您的问题

此解决方案首先在用户的日历中创建一个事件,然后在事件应该发生之前的一些(指定)分钟通过通知通知用户。

要将事件添加到用户的日历,您可以指定开始日期和时间、结束日期和时间、事件标题以及有关事件的任何其他信息(带有注释)。

fun addEventToCalendar(date: String, time: String, notifyTime: Int = 0, title: String, note: String) {
        // Make sure to which calender you want to add event
        val calID: Long = 1 // primary calendar
        val beginTime = Calendar.getInstance()
        val simpleDateFormat = SimpleDateFormat("MM-dd-yyyy HH:mm", Locale.ENGLISH)
        beginTime.time = simpleDateFormat.parse("$date $time")
        val startMillis = beginTime.timeInMillis
        val endMillis = startMillis + (30 * 60 * 1000) //in this snippet the endtime is 30 minutes after the start time

        val cr = context.contentResolver
        val values = ContentValues()
        values.put(CalendarContract.Events.DTSTART, startMillis)
        values.put(CalendarContract.Events.DTEND, endMillis)
        values.put(CalendarContract.Events.TITLE, title)
        values.put(CalendarContract.Events.DESCRIPTION, note)
        values.put(CalendarContract.Events.CALENDAR_ID, calID)
        values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().id)
        values.put(CalendarContract.Events.HAS_ALARM, 1)
        val uri = cr.insert(CalendarContract.Events.CONTENT_URI, values) //insert the event into the user's calendar here 

        // get the event ID that is the last element in the Uri
        val eventID = uri!!.lastPathSegment!!.toLong()
        setReminderForEvent(eventID, notifyTime)
}

要向事件添加提醒,需要使用 EventID 来标识您要添加提醒的确切事件。 eventID 包含在将事件添加到用户日历后返回的 uri 中。

     val uri = cr.insert(CalendarContract.Events.CONTENT_URI, values) 
        // get the event ID that is the last element in the Uri
     val eventID = uri!!.lastPathSegment!!.toLong()

添加提醒时,您可以指定您希望在事件发生前多少分钟通知用户。

在此代码片段中,notifyTime 用于指定您要在事件发生前多少分钟通知用户

 private fun setReminderForEvent(eventID: Long, notifyTime: Int){
        val otherValues = ContentValues().apply {
            put(CalendarContract.Reminders.MINUTES, notifyTime)
            put(CalendarContract.Reminders.MINUTES, 0)
            put(CalendarContract.Reminders.EVENT_ID, eventID)
            put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT)
        }
        context.contentResolver.insert(CalendarContract.Reminders.CONTENT_URI, otherValues) // outputs a URI
    }

希望这有帮助

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.

fun addEventToCalendar(date: String, time: String, notifyTime: Int = 0, title: String, note: String) {
        // Make sure to which calender you want to add event
        val calID: Long = 1 // primary calendar
        val beginTime = Calendar.getInstance()
        val simpleDateFormat = SimpleDateFormat("MM-dd-yyyy HH:mm", Locale.ENGLISH)
        beginTime.time = simpleDateFormat.parse("$date $time")
        val startMillis = beginTime.timeInMillis
        val endMillis = startMillis + (30 * 60 * 1000) //in this snippet the endtime is 30 minutes after the start time

        val cr = context.contentResolver
        val values = ContentValues()
        values.put(CalendarContract.Events.DTSTART, startMillis)
        values.put(CalendarContract.Events.DTEND, endMillis)
        values.put(CalendarContract.Events.TITLE, title)
        values.put(CalendarContract.Events.DESCRIPTION, note)
        values.put(CalendarContract.Events.CALENDAR_ID, calID)
        values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().id)
        values.put(CalendarContract.Events.HAS_ALARM, 1)
        val uri = cr.insert(CalendarContract.Events.CONTENT_URI, values) //insert the event into the user's calendar here 

        // get the event ID that is the last element in the Uri
        val eventID = uri!!.lastPathSegment!!.toLong()
        setReminderForEvent(eventID, notifyTime)
}

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.

     val uri = cr.insert(CalendarContract.Events.CONTENT_URI, values) 
        // get the event ID that is the last element in the Uri
     val eventID = uri!!.lastPathSegment!!.toLong()

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

 private fun setReminderForEvent(eventID: Long, notifyTime: Int){
        val otherValues = ContentValues().apply {
            put(CalendarContract.Reminders.MINUTES, notifyTime)
            put(CalendarContract.Reminders.MINUTES, 0)
            put(CalendarContract.Reminders.EVENT_ID, eventID)
            put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT)
        }
        context.contentResolver.insert(CalendarContract.Reminders.CONTENT_URI, otherValues) // outputs a URI
    }

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文