Swift iOS 9.2中的每日本地通知 [英] Daily Local Notification In Swift iOS 9.2
问题描述
尝试在swift中发送每日本地通知。然而它只是出于某种原因发送每分钟。我希望第一个通知在应用程序打开后发送30分钟,然后每天重复此通知。
Trying to send a daily local notification in swift. However it just sends every minute for some reason. I want the first notification to send 30 mins after the app is opened and then repeat this notification everyday.
在swift fie我有以下代码:
in the swift fie i have the following code:
// ---------每日通知代码(也在app delagate中添加部分----------
让theDate = NSDate ()
//---------Daily notification code (also add section in app delagate---------- let theDate = NSDate()
let dateComp = NSDateComponents()
dateComp.minute = 30
let cal = NSCalendar.currentCalendar()
let fireDate:NSDate = cal.dateByAddingComponents(dateComp , toDate: theDate, options: NSCalendarOptions(rawValue: 0))!
let notification:UILocalNotification = UILocalNotification()
//choosing what it says in the notification and when it fires
notification.alertBody = "Your Daily Motivation is Awaits"
notification.fireDate = fireDate
UIApplication.sharedApplication().scheduleLocalNotification(notification)
//displaying notification every day
notification.repeatInterval = NSCalendarUnit.Day
//-------------end of daily notification code---------------
在我的app委托文件中具有以下代码:
in my app delegate file i have the following code:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//----------daily notification section ----------
let notiftypes:UIUserNotificationType = UIUserNotificationType.Alert.union(UIUserNotificationType.Badge).union(UIUserNotificationType.Sound)
let notifSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notiftypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notifSettings)
return true
//----------end of daily notification section-----------
}
推荐答案
问题是您在安排后设置重复间隔通知。只需在scheduleLocalNotification之前设置通知属性,并确保只安排一次:
The problem is that you are setting the repeat interval after scheduling the notification. Just set the notification property before scheduleLocalNotification and make sure you schedule it only once:
let notification = UILocalNotification()
notification.alertBody = "Your Daily Motivation is Awaits"
// You should set also the notification time zone otherwise the fire date is interpreted as an absolute GMT time
notification.timeZone = NSTimeZone.localTimeZone()
// you can simplify setting your fire date using dateByAddingTimeInterval
notification.fireDate = NSDate().dateByAddingTimeInterval(1800)
// set the notification property before scheduleLocalNotification
notification.repeatInterval = .Day
UIApplication.sharedApplication().scheduleLocalNotification(notification)
注意:UIUserNotificationType是OptionSetType结构,所以你也可以简化它的声明:
Note: UIUserNotificationType are OptionSetType structures, so you can simplify its declaration also:
let notiftypes:UIUserNotificationType = [.Alert, .Badge, .Sound]
这篇关于Swift iOS 9.2中的每日本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!