在一周的特定日期重复本地通知(Swift 3 IOS 10)

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

我正在尝试安排一周中特定日期(例如星期一,星期三等)的本地通知,然后每周重复一次. 这是用于设置通知的屏幕的外观:

I am trying to schedule local notifications for specific days of week (e.g. Monday, Wednesday and etc) and then repeat them weekly. This is how the screen for setting notifications looks:

用户可以选择通知时间和重复天数.

User can select time for the notification and repeating days.

我安排单个非重复通知的方法如下:

My method for scheduling single non repeating notification looks like this:

static func scheduleNotification(reminder: Reminder) { // Setup notification content. let content = UNMutableNotificationContent() //content.title = NSString.localizedUserNotificationString(forKey: "Reminder", arguments: nil) content.body = NSString.localizedUserNotificationString(forKey: reminder.reminderMessage, arguments: nil) content.sound = UNNotificationSound.default() // Configure the triger for specified time. // let dateComponentes = reminder.dateComponents // TODO: Configure repeating alarm // For the testing purposes we will not repeat the reminder let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponentes, repeats: false) // Create the request object. let request = UNNotificationRequest(identifier: "\(reminder.reminderId)", content: content, trigger: trigger) // Schedule the request. let notificationCenter = UNUserNotificationCenter.current() notificationCenter.add(request) { (error: Error?) in if let theError = error { print(theError.localizedDescription) } } }

从UIDatePicker小部件中提取日期成分,并将其存储在提醒类中:

The date components are extracted from UIDatePicker widget and stored in reminder class:

let date = reminderTimeDatePicker.date.addingTimeInterval(60 * 60 * 24 * 7) let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: date) ... reminder.dateComponents = components

我有一个数组selectedDays[Int](作为提醒类的一个属性),用于保存应该在一周中的几天触发通知的信息.

I have an array selectedDays[Int] (as a property of reminder class) to hold info on days of week on which the notification should fire.

我如何安排在一周的特定日期进行通知,以及如何每周重复一次?

How can I schedule notification on specific day of week and how to repeat it weekly?

即使发表任何评论也将有所帮助,并在此先感谢您.

Even a single comment will be helpful, and thank you in advance.

推荐答案

您可以使用以下函数从选定的选择器值中获取日期:

You can use below function to get Date from selected picker value:

//Create Date from picker selected value. func createDate(weekday: Int, hour: Int, minute: Int, year: Int)->Date{ var components = DateComponents() components.hour = hour components.minute = minute components.year = year components.weekday = weekday // sunday = 1 ... saturday = 7 components.weekdayOrdinal = 10 components.timeZone = .current let calendar = Calendar(identifier: .gregorian) return calendar.date(from: components)! } //Schedule Notification with weekly bases. func scheduleNotification(at date: Date, body: String, titles:String) { let triggerWeekly = Calendar.current.dateComponents([.weekday,.hour,.minute,.second,], from: date) let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true) let content = UNMutableNotificationContent() content.title = titles content.body = body content.sound = UNNotificationSound.default() content.categoryIdentifier = "todoList" let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger) UNUserNotificationCenter.current().delegate = self //UNUserNotificationCenter.current().removeAllPendingNotificationRequests() UNUserNotificationCenter.current().add(request) {(error) in if let error = error { print("Uh oh! We had an error: \(error)") } } }

从选择器通行证选择器的小时,分​​钟和年份中获取一个值,并将选定的工作日设为(星期日= 1,星期一= 2,星期二= 3,星期三= 4,星期四= 5,星期五= 6,星期六= 7)可以使用功能func createDate(weekday: Int, hour: Int, minute: Int, year: Int)来获取每周的通知开始日期,并在获取日期后调用功能func scheduleNotification(at date: Date, body: String, titles:String)来安排通知.

After getting a value from picker pass picker hour, minute and year with selected week day as (Sunday = 1, Monday = 2, Tuesday = 3, Wednesday = 4, thursday = 5, Friday = 6, Saturday = 7) to function func createDate(weekday: Int, hour: Int, minute: Int, year: Int) to get notification fire date on weekly bases, and after getting date call function func scheduleNotification(at date: Date, body: String, titles:String) for schedule a notification.

更多推荐

在一周的特定日期重复本地通知(Swift 3 IOS 10)

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

发布评论

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

>www.elefans.com

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