如何在一周中的特定日期重复本地通知(iOS Swift 3) [英] How to repeat local notifications on specific days of the week (iOS Swift 3)

查看:100
本文介绍了如何在一周中的特定日期重复本地通知(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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆