如何安排每周两天的本地通知?

时间:2019-09-02 09:22:53

标签: ios swift notifications

我想安排每周发送两次通知:

let gregorian = Calendar(identifier: .gregorian)
let now = Date()

[2, 4].forEach { day in 
    let content = UNMutableNotificationContent()
    content.title = "Notif title"
    content.body = "Comment"
    content.sound = .default
    var components = gregorian.dateComponents([.year, .month, .weekdayOrdinal, .day, .hour, .minute, .second], from: now)
    components.hour = 11
    components.minute = 25
    components.second = 0
    components.weekdayOrdinal = day

    let date = gregorian.date(from: components)!
    let triggerDaily = gregorian.dateComponents([.weekdayOrdinal, .hour, .minute, .second], from: date)
    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
    let request = UNNotificationRequest(identifier: "test", content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request) { error in
        if let error = error {
            print("Oops: \(error)")
        } else {
            print("Notification created!")
        }
    }
}

但不是每天星期一和星期三一次被触发,而是每天被调用两次。

我在做什么错?感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

您可以使用.weekday而不是.weekdayOrdinal。根据Apple文件

  

.weekday =工作日或工作日计数。

     

.weekdayOrdinal =工作日序数或工作日序数的计数。   工作日的序数单位代表工作日在广告中的位置   下一个较大的日历单位,例如月份。例如2是   该月第二个星期五的工作日序数单位。

尝试一下

 let triggerDate =  Calendar.current.dateComponents([.weekday,.hour,.minute], from: date as Date)
triggerDat.weekday = day[index]

 let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate,
                                                            repeats: true)

答案 1 :(得分:0)

因为abstract class Base { public abstract function doSql($version); } class MySQL extends Base { public function doSql($version) { switch($version) { case 1 : break; case 2 : break; } } } class Oracle extends Base { public function doSql($version) { switch($version) { case 1 : break; case 2 : break; } } } 循环了您的代码以相同的标识符调度两个通知。当您的代码开始执行时,第二个循环周期将覆盖请求。

我做了一些研发工作,并对代码进行了一些改进。在我这方面,它运作良好。

请尝试下面的代码。

foreach

用您的func setNotification() { let days = [2,4] func ScheduleNotification(index:Int){ let calendarComponents = NSDateComponents() calendarComponents.hour = 11 calendarComponents.minute = 25 calendarComponents.second = 0 calendarComponents.weekday = days[index] let content = UNMutableNotificationContent() content.title = "Notification title" content.body = "your message here." content.sound = UNNotificationSound.default let trigger = UNCalendarNotificationTrigger(dateMatching: calendarComponents as DateComponents, repeats: true) let identifier = "UYLLocalNotification\(days[index])" let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger) UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in if let error = error { print(error) }else{ if index == 0{ ScheduleNotification(index: 1) } } }) } ScheduleNotification(index: 0) } 方法调用此函数。

viewdidLoad

我希望它可以节省您的时间。

答案 2 :(得分:0)

只需将identifier中每个通知的foreach(_:)的名称更改为test2,test4之类的名称,即

[2, 4].forEach {
    let content = UNMutableNotificationContent()
    content.title = "title"
    content.body = "body"
    content.sound = .default

    var components = DateComponents()
    components.timeZone = TimeZone.current
    components.weekday = 11
    components.hour = 25
    components.minute = 0
    components.second = $0

    let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
    let request = UNNotificationRequest(identifier: "test\($0)", content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request) { error in
        if let error = error {
            print("Oops: \(error)")
        } else {
            print("Notification created!")
        }
    }
}