Swift iOS 9.2 中的每日本地通知
Daily Local Notification In Swift iOS 9.2
正在尝试在 swift 中发送每日本地通知。然而,由于某种原因它只是每分钟发送一次。我希望应用程序打开后 30 分钟发送第一个通知,然后每天重复此通知。
在 swift 文件中,我有以下代码:
//--------每日通知代码(同时在app delagate中添加部分--------
让 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---------------
在我的应用程序委托文件中,我有以下代码:
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 之前设置通知 属性 并确保只安排一次:
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 结构,所以你也可以简化它的声明:
let notiftypes:UIUserNotificationType = [.Alert, .Badge, .Sound]
正在尝试在 swift 中发送每日本地通知。然而,由于某种原因它只是每分钟发送一次。我希望应用程序打开后 30 分钟发送第一个通知,然后每天重复此通知。
在 swift 文件中,我有以下代码:
//--------每日通知代码(同时在app delagate中添加部分-------- 让 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---------------
在我的应用程序委托文件中,我有以下代码:
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 之前设置通知 属性 并确保只安排一次:
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 结构,所以你也可以简化它的声明:
let notiftypes:UIUserNotificationType = [.Alert, .Badge, .Sound]