简体   繁体   English

Swift iOS 9.2中的每日本地通知

[英]Daily Local Notification In Swift iOS 9.2

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. 我希望第一个通知在应用打开后30分钟内发送,然后每天重复此通知。

in the swift fie i have the following code: 在迅速的国际剑联,我有以下代码:

//---------Daily notification code (also add section in app delagate---------- let theDate = NSDate() // ---------每日通知代码(也在应用程序滞后中添加部分)------------让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---------------

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-----------
}

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: 只需在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)

Note: UIUserNotificationType are OptionSetType structures, so you can simplify its declaration also: 注意:UIUserNotificationType是OptionSetType结构,因此您还可以简化其声明:

let notiftypes:UIUserNotificationType = [.Alert, .Badge, .Sound]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM