如何在每天的上班时间重复本地通知?

时间:2020-04-01 11:12:45

标签: ios swift

我在google上进行了研究,并为我的应用创建了本地通知(“洗手提醒”),但我认为我在操作上犯了一个错误,因为我将其设置为每天08:00 AM-10:00 PM之间,但它发送了第一条通知在9:00 AM,但在10:00不再重复。

UI屏幕截图将帮助您了解我想做什么。

enter image description here

功能:

0

AppDelegate.swift:

 func createNotify(){
    let now = Date().shortTime
    if now < endTime.shortTime{

        let content = UNMutableNotificationContent()
        content.title = titles.randomElement()!
        content.body = contents.randomElement()!
        content.sound = UNNotificationSound.default

        let interval:TimeInterval = 3600.0 // 1 minute = 60 seconds
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: true)
        let request = UNNotificationRequest(identifier:"washHands", content: content, trigger: trigger)

        UNUserNotificationCenter.current().add(request)
    }else{
        print("do nothing")
    }
}

1 个答案:

答案 0 :(得分:2)

default_random_engine RandomNumber{std::random_device{}()};

使用let interval:TimeInterval = 60.0 // 1 minute = 60 seconds let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: true) 代替UNTimeIntervalNotificationTrigger,因为要在指定的日期和时间安排本地通知的传递时使用UNCalendarNotificationTrigger对象,而UNCalendarNotificationTrigger对象是在您希望在指定的秒数后安排传递本地通知时使用。

https://developer.apple.com/documentation/usernotifications/untimeintervalnotificationtrigger