栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

即使每天的特定时间过去了,如何发送本地通知?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

即使每天的特定时间过去了,如何发送本地通知?

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

/// Set up the local notification for everyday/// - parameter hour: The hour in 24 of the day to trigger the notificationclass 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);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393357.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号