如何在一周中的特定日期重复本地通知(iOS Swift 3)

编程入门 行业动态 更新时间:2024-10-27 05:22:38
本文介绍了如何在一周中的特定日期重复本地通知(iOS Swift 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建我的第一个应用程序,让用户为自己设置警报,然后应用程序会在设置的时间向用户发送本地通知。我现在遇到麻烦的事情是能够在一周的特定日期(例如每个星期一和星期二)发出本地通知。我所遇到到目前为止(像的这个只是关于每天/每周安排通知。我想要实现的结果有点像iPhone的内置报警系统,你可以在一周中的某几天设置一个报警器。

I am creating my first app that lets the user set alerts for themselves and then the app would send the user a local notification on the set time. The thing that I'm having trouble with now is being able to fire the local notification on specific days of the week (for example every Mondays and Tuesdays only). All the tutorials/questions that I have come across so far (like this one) have only been about scheduling the notification everyday/everyweek. The desired result that I'm trying to achieve is sort of like the iPhone's built in alarm system, where you can set an alarm to fire only on certain days of the week.

如果我不能只设置 notification.repeatInterval 字段,有没有办法可以做我想做的事情?我可以在午夜执行一些代码来安排当天的正确通知吗?如果是这样,我怎么能这样做?

If I can't just set the notification.repeatInterval field, is there a way I can do what I'm trying to do? Could I possibly execute a little code at midnight that schedules the correct notifications for that day? If so, how could I do that?

提前致谢!

推荐答案

我现在遇到的问题是能够在一周中的特定日期触发本地通知

The thing that I'm having trouble with now is being able to fire the local notification on specific days of the week

在iOS 10中,使用DateComponents形成UNCalendarNotificationTrigger。这意味着您可以根据需要指定相关日期时间的多少。因此,如果您指定特定的工作日和时间(小时和分钟),而不是其他任何内容,那么您将在一周中的那一天重复此操作。

In iOS 10, a UNCalendarNotificationTrigger is formed using DateComponents. This means you can specify as much or as little of the date-time in question as you wish. Thus, if you specify a specific weekday and a time (hour and minutes), and nothing else, you'll repeat at that time on that day of the week.

如果您还需要在一周的另一天重复,请再次发出通知。

If you also need to repeat on a different day of the week, just make another notification.

更多推荐

如何在一周中的特定日期重复本地通知(iOS Swift 3)

本文发布于:2023-11-27 02:51:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636260.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   通知   Swift   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!