【问题标题】:Repeating local notifications for specific days of week (Swift 3 IOS 10)在一周中的特定日子重复本地通知(Swift 3 IOS 10)
【发布时间】:2017-12-17 02:19:20
【问题描述】:

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

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

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

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 小部件中提取日期组件并存储在提醒类中:

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

我有一个数组selectedDays[Int](作为提醒类的属性)来保存应该触发通知的星期几的信息。

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

即使是一条评论也会有所帮助,并提前感谢您。

【问题讨论】:

标签: ios swift swift3 notifications


【解决方案1】:

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

    //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) 以安排通知。

【讨论】:

  • 在我的问题中包含的图片中,可以看出用户可以选择一周中的几天来重复单个通知,例如在星期三和星期五重复通知。因此,为了让单个通知在一周内重复超过一天,我是否需要为每天的弱点创建单独的通知,如果需要,是否可以为多个通知使用相同的标识符?跨度>
  • 是的,您必须为一周中的每一天创建单独的通知,并且每个通知的标识符应该是唯一的,如果您在安排新通知时使用相同的标识符,系统会删除以前安排的通知该标识符并将其替换为新标识符。
  • 对于components.weekdayOrdinal的使用我不是很确定,能解释一下吗?我已经查过了,但仍然不确定它在这里的用途。
  • weekdayOrdinal 返回接收方的工作日单位序数,其中工作日序数单位是“工作日在下一个较大日历单位(例如月份)中的位置。例如,2 是工作日每月第二个星期五的序数单位。这里使用 weekdayOrdinal 是可选的。
【解决方案2】:
 let content = UNMutableNotificationContent()
 content.title = NSString.localizedUserNotificationString(forKey: "Wake up!", arguments: nil)
 content.body = NSString.localizedUserNotificationString(forKey: "Rise and shine! It's morning time!",
                                                                arguments: nil)
 content.categoryIdentifier = "TIMER_EXPIRED"

 let weekdaySet = [6,5]

   for i in weekdaySet {

        var dateInfo = DateComponents()
        dateInfo.hour = 16
        dateInfo.minute = 44
        dateInfo.weekday = i
        dateInfo.timeZone = .current

        let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: true)
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        center.add(request) { (error : Error?) in

        if let theError = error {
                print(theError.localizedDescription)
                }
            }
       }

【讨论】:

  • 所以它会在周五和周六不断重复?以及它将如何停止?
  • 嗨!,我正在使用相同的功能,但没有收到通知,甚至没有重复通知。
猜你喜欢
  • 2017-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
相关资源
最近更新 更多