如何在swift 3中每天在特定时间触发本地通知
Posted
技术标签:
【中文标题】如何在swift 3中每天在特定时间触发本地通知【英文标题】:How to trigger a local notification at particular time daily in swift 3 【发布时间】:2016-11-10 05:45:46 【问题描述】:我是 ios 开发的新手,但已经创建了应用程序,并且我正在尝试为特定时间(例如(上午 10 点))创建每日通知。如果我将移动设备时间设置为上午 10 点,现在通知会太多。我只想在给定的特定时间(即上午 10 点)触发一次本地通知,并且我想每天重复一次。每天重复通知的最佳方法是什么?
这是我的代码
func fire()
let dateComp:NSDateComponents = NSDateComponents()
dateComp.hour = 10
dateComp.minute = 00
dateComp.timeZone = NSTimeZone.systemTimeZone()
let calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierIndian)!
let date:NSDate = calender.dateFromComponents(dateComp)!
let localNotificationSilent = UILocalNotification()
localNotificationSilent.fireDate = date
localNotificationSilent.repeatInterval = NSCalendarUnit.Day
localNotificationSilent.alertBody = "Started!"
localNotificationSilent.alertAction = "swipe to hear!"
localNotificationSilent.timeZone = NSCalendar.currentCalendar().timeZone
localNotificationSilent.category = "PLAY_CATEGORY"
UIApplication.sharedApplication().scheduleLocalNotification(localNotificationSilent)
【问题讨论】:
Repeating local notification daily at a set time with swift的可能重复 【参考方案1】:在 Swift 3、iOS 10 中工作:
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert, .sound, .badge], completionHandler: userDidAllow, error in
//if userDidAllow : do something if you want to
)
//Set notification to trigger 11:30AM everyday
var dateComponents = DateComponents()
dateComponents.hour = 11
dateComponents.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
//Set your content
let content = UNMutableNotificationContent()
content.title = "Your notification title"
content.body = "Your notification body"
let request = UNNotificationRequest(
identifier: "yourIdentifier", content: content, trigger: trigger
)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
【讨论】:
嘿,我无法在特定时间触发通知。我做了同样的代码。【参考方案2】:您的问题是开火日期不正确
你可以像这样创建开火日期
let today = Date()
let calendar = NSCalendar.current
let components = calendar.dateComponents([.day, .month, .year], from: today)
var dateComp:DateComponents = DateComponents()
dateComp.day = components.day
dateComp.month = components.month
dateComp.year = components.year
dateComp.hour = 10
dateComp.minute = 00
let date = calendar.date(from: dateComp)
如果你想验证开火日期,那么你可以这样检查
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MM yyyy hh:mm:ss"
let fireDate = dateFormatter.string(from: date!)
print("fireDate: \(fireDate)")
现在是时候为本地通知设置触发日期了
localNotificationSilent.fireDate = date
// no need to set time zone Remove bellow line
localNotificationSilent.timeZone = NSCalendar.currentCalendar().timeZone
通知代码
let localNotificationSilent = UILocalNotification()
localNotificationSilent.fireDate = date
localNotificationSilent.repeatInterval = .day
localNotificationSilent.alertBody = "Started!"
localNotificationSilent.alertAction = "swipe to hear!"
localNotificationSilent.category = "PLAY_CATEGORY"
UIApplication.shared.scheduleLocalNotification(localNotificationSilent)
希望对你有帮助。
【讨论】:
如何每天重复?? let today = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Day, .Month, .Year], fromDate: today) let dateComp:NSDateComponents = NSDateComponents () dateComp.day = components.day dateComp.month = components.month dateComp.year = components.year dateComp.hour = 10 dateComp.minute = 00 let date = calendar.dateFromComponents(dateComp) 我已经更改了这样的代码 bez ur代码 dosent 支持 swift3,如果我将设备设置为上午 10 点,现在会收到 3 个通知 @jigneshVadadoriya 如何从上面的代码限制周六和周日的通知。我的意思是不想在周六和周日开火。 @User558 您需要更改上面的代码,例如 localNotificationSilent.repeatInterval = .weekday 或 localNotificationSilent.repeatInterval = .weekdayOrdinal 可能会对您有所帮助以上是关于如何在swift 3中每天在特定时间触发本地通知的主要内容,如果未能解决你的问题,请参考以下文章