如何在每天的特定时间发送本地通知,即使那个时间已经过去?

Posted

技术标签:

【中文标题】如何在每天的特定时间发送本地通知,即使那个时间已经过去?【英文标题】:How to send a localNotification at a specific time everyday, even if that time has passed? 【发布时间】:2015-08-05 00:02:20 【问题描述】:

我有这段代码,它每天早上 7 点运行通知,它获取当前日期,然后在到达设定时间时运行通知,我的问题是如果时间已经超过设定的运行时间,那么每天都会在用户当前时间而不是我早上 7 点的时间运行,这是我的代码

var dateFire: NSDateComponents = NSDateComponents()
var getCurrentYear = dateFire.year
var getCurrentMonth = dateFire.month
var getCurrentDay = dateFire.day

dateFire.year = getCurrentYear
dateFire.month = getCurrentMonth
dateFire.day = getCurrentDay
dateFire.hour = 7
dateFire.minute = 0
dateFire.timeZone = NSTimeZone.defaultTimeZone()


var calender: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var date: NSDate = calender.dateFromComponents(dateFire)!

var localNotification = UILocalNotification()
localNotification.fireDate = date
localNotification.alertBody = "A new day has begun and a fresh layer on snow lies on the mountain! Can you beat your highscore?"
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

如您所见,NSCalendarUnit.CaldendarUnitDay 每天早上 7 点运行。我不知道,即使时间是早上 7 点之后,通知仍然会在第二天运行,将不胜感激

【问题讨论】:

您需要检查当前时间 - 如果是在 7:00 之后,请将开火日期设置为明天,而不是今天。 【参考方案1】:

Swift 5.1 例如,这个每天早上 8:00 触发。 :

let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Title"
notificationContent.body = "This is a test"
notificationContent.badge = NSNumber(value: 1)
notificationContent.sound = .default
                
var datComp = DateComponents()
datComp.hour = 8
datComp.minute = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: datComp, repeats: true)
let request = UNNotificationRequest(identifier: "ID", content: notificationContent, trigger: trigger)
                UNUserNotificationCenter.current().add(request)  (error : Error?) in
                    if let theError = error 
                        print(theError.localizedDescription)
                    
                

【讨论】:

随心所欲...您可以将其连接到按钮,将其包装成函数等。 只需将print(theError as! String) 切换到print(theError.localizedDescription) 就完美了! @MumtazHussain 我感觉到你的痛苦。很少有针对 ios 问题的 *** 答案指定实际放置代码的位置。【参考方案2】:

更新了@Paulw11 对 Swift 3.0 的回答并包含在一个函数中:

/// Set up the local notification for everyday
/// - parameter hour: The hour in 24 of the day to trigger the notification
class func setUpLocalNotification(hour: Int, minute: Int) 

    // have to use NSCalendar for the components
    let calendar = NSCalendar(identifier: .gregorian)!;

    var dateFire = Date()

    // if today's date is passed, use tomorrow
    var fireComponents = calendar.components( [NSCalendar.Unit.day, NSCalendar.Unit.month, NSCalendar.Unit.year, NSCalendar.Unit.hour, NSCalendar.Unit.minute], from:dateFire)

    if (fireComponents.hour! > hour 
        || (fireComponents.hour == hour && fireComponents.minute! >= minute) ) 

        dateFire = dateFire.addingTimeInterval(86400)  // Use tomorrow's date
        fireComponents = calendar.components( [NSCalendar.Unit.day, NSCalendar.Unit.month, NSCalendar.Unit.year, NSCalendar.Unit.hour, NSCalendar.Unit.minute], from:dateFire);
    

    // set up the time
    fireComponents.hour = hour
    fireComponents.minute = minute

    // schedule local notification
    dateFire = calendar.date(from: fireComponents)!

    let localNotification = UILocalNotification()
    localNotification.fireDate = dateFire
    localNotification.alertBody = "Record Today Numerily. Be completely honest: how is your day so far?"
    localNotification.repeatInterval = NSCalendar.Unit.day
    localNotification.soundName = UILocalNotificationDefaultSoundName;

    UIApplication.shared.scheduleLocalNotification(localNotification);


【讨论】:

能否根据应用收集的数据每天更改该消息? 您愿意回答这个问题吗?请***.com/questions/49666018/…【参考方案3】:

您需要检查当前时间,如果晚于 6:59,您需要安排明天的通知 - 这将阻止您的通知安排在过去 -

var calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var dateFire=NSDate()

var fireComponents=calendar.components(NSCalendarUnit.CalendarUnitDay|NSCalendarUnit.CalendarUnitMonth|NSCalendarUnit.CalendarUnitYear|NSCalendarUnit.CalendarUnitHour|NSCalendarUnit.CalendarUnitMinute, fromDate:dateFire)

if (fireComponents.hour >= 7) 
    dateFire=dateFire.dateByAddingTimeInterval(86400)  // Use tomorrow's date

    fireComponents=calendar.components(NSCalendarUnit.CalendarUnitDay|NSCalendarUnit.CalendarUnitMonth|NSCalendarUnit.CalendarUnitYear|NSCalendarUnit.CalendarUnitHour|NSCalendarUnit.CalendarUnitMinute, fromDate:dateFire)



fireComponents.hour = 7
fireComponents.minute = 0

dateFire = calendar.dateFromComponents(fireComponents)!

var localNotification = UILocalNotification()
localNotification.fireDate = dateFire
localNotification.alertBody = "A new day has begun and a fresh layer on snow lies on the mountain! Can you beat your highscore?"
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

【讨论】:

当我添加这个时,它说 CalendarUnit 没有 CalendarUnitDay 的成员 这是 Swift 3 吗?【参考方案4】:

更新版本(Swift 2.0)

    let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    var dateFire=NSDate()

    var fireComponents=calendar.components([.Month, .Day, .Hour, .Minute], fromDate:NSDate())

    if (fireComponents.hour == 7) 
        dateFire=dateFire.dateByAddingTimeInterval(86400)  // Use tomorrow's date

        fireComponents=calendar.components([.Month, .Day, .Hour, .Minute], fromDate:NSDate())
    


    fireComponents.hour = 7
    fireComponents.minute = 0

    dateFire = calendar.dateFromComponents(fireComponents)!

    let localNotification = UILocalNotification()
    localNotification.fireDate = dateFire
    localNotification.alertBody = "Don't forget to visit Quote Daily to receive daily motivation."
    localNotification.repeatInterval = NSCalendarUnit.Day

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

【讨论】:

更新版本是什么? 使用 swift 2.0 @Braiam ,当使用上面的答案时,你会在使用:(NSCalendarUnit.CalendarUnitDay|NSCalendarUnit.CalendarUnitMonth|NSCalendarUnit.CalendarUnitYear|NSCalendarUnit.CalendarUnitHour|NSCalendarUnit.CalendarUnitMinute, fromDate:dateFire) 的行上遇到问题,而不是这个,你必须插入:var fireComponents=calendar.components([.Month, .Day, .Hour, .Minute], fromDate:NSDate())

以上是关于如何在每天的特定时间发送本地通知,即使那个时间已经过去?的主要内容,如果未能解决你的问题,请参考以下文章

本地通知在错误的时间发送

如何在每天早上 8 点到晚上 8 点之间设置本地通知

如何使用cordova-plugin-local-notification在离子的特定时间每天获取本地通知

在特定的时间段内每天以每小时间隔运行本地通知

如何在 swift [iOS] 中从特定时间开始发送本地通知 UNTimeInterval

如何在本地安排 React 中的重复网络通知?