问题描述
我想为我的应用设置每日通知系统.它应该每天两次通知用户,一次是在8:00 AM(嘿,您的早晨剂量已经准备好了.要检查吗?),一次是在7:00 PM(Supup!您的晚上剂量正在里面等待). .我知道这样做会导致一个月内的通知用尽,因为有64个通知上限(用于本地通知),但是到那时,该应用程序将处于活动状态,并且我将完成设置远程通知更新.
I want to setup a daily notification system for my app. It should notify the user twice a day, once at 8:00 AM (Hey there, your set of morning doses is ready. Wanna check it out?) and once at 7:00 PM (Ssup! Your evening Dose is waiting inside). I know that by doing this i'll run out of notifications in a month since there's a 64 notifications cap (for local notifications) but by then, the app will be live and i'll be done setting up remote notifications update.
我查看了以下问题:如何安排迅速地发出相同的本地通知,并且.Day都很好.我只需要知道如何在指定的时间每天两次.预先感谢!
I've looked at this question: How to schedule a same local notification in swift and the .Day is all good. I just need to know how to do it twice a day at those specified times;. Thanks in advance!
编辑:如何设置通知的特定时间是问题的一部分. NSDate API给了我有趣的时间间隔(sinceNow/Since1973).我只想将其设置为在晚上7点启动. :-/除非我缺少某些东西,否则似乎无法使用这些NSDate api做到这一点.
how to set the specific time on the notification is part of the problem. The NSDate API is giving me funny time intervals (sinceNow/Since1973). I just want to set it to fire at 7 PM. :-/ Can't quite seem to do it with these NSDate apis unless I'm missing something.
推荐答案
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let settings = UIUserNotificationSettings(forTypes: .Badge, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
let localNotification1 = UILocalNotification()
localNotification1.alertBody = "Your alert message 111"
localNotification1.timeZone = NSTimeZone.defaultTimeZone()
localNotification1.fireDate = self.getEightAMDate()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification1)
let localNotification2 = UILocalNotification()
localNotification2.alertBody = "Your alert message22"
localNotification2.timeZone = NSTimeZone.defaultTimeZone()
localNotification2.fireDate = self.getSevenPMDate()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification2)
return true
}
func getEightAMDate() -> NSDate? {
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now: NSDate! = NSDate()
let date10h = calendar.dateBySettingHour(8, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!
return date10h
}
func getSevenPMDate() -> NSDate? {
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now: NSDate! = NSDate()
let date19h = calendar.dateBySettingHour(19, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!
return date19h
}
这篇关于在一天中的特定时间安排本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!