Notification locale quotidienne dans Swift iOS 9.2

Essayer d'envoyer une notification locale quotidienne dans swift. Cependant, il envoie juste chaque minute pour une raison quelconque. Je veux que la première notification soit envoyée 30 minutes après l'ouverture de l'application, puis répétez cette notification tous les jours.

dans le swift fie j'ai le code suivant:

// ——— Code de notification journalier (ajoutez également la section dans l'application 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--------------- 

dans mon file délégué de l'application, j'ai le code suivant:

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

Le problème est que vous définissez l'intervalle de répétition après la planification de la notification. Définissez simplement la propriété de notification avant scheduleLocalNotification et assurez-vous de ne la programmer qu'une seule fois:

 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) 

Remarque: UIUserNotificationType sont des structures OptionSetType, vous pouvez donc également simplifier sa déclaration:

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