iOS 8 .scheduledLocalNotifications vide

J'essaie de mettre à jour mon application à iOS 8. Dans une fonction, je prévois une notification locale (j'ai déjà vérifié que firedate et toutes les autres parties de la notification ont raison) de cette façon:

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

Ensuite, j'utilise ce code pour imprimer la notification locale programmée:

 NSLog(@"notifications %@", [UIApplication sharedApplication].scheduledLocalNotifications ); 

mais le tableau

 [UIApplication sharedApplication].scheduledLocalNotifications 

est vide même si la notification n'est pas déclenchée. Ensuite, pour vérifier si la notification locale est vraiment planifiée, j'ai essayé d'utiliser le code

 NSLog(@"notification appdelegate %@", application.scheduledLocalNotifications ); 

dans la fonction

 - (void)applicationWillResignActive:(UIApplication *)application 

de Appdelegate.m dans ce cas, le tableau des notifications locales planifiées n'est pas vide et la fonction NSLog imprime la notification correcte. Cela se produit uniquement sur les appareils réels, dans le simulateur mon application fonctionne très bien. Et le problème n'est pas de vérifier la permission de l'user de programmer des notifications locales, parce que je l'ai déjà fait face. Quelqu'un pourrait-il m'aider? Quelques idées?

Ce n'est pas iOS 8, mais

 [UIApplication sharedApplication].scheduledLocalNotifications 

problème. La réponse la plus pertinente que je trouve ici

Avoir des problèmes similaires en ce moment. Ma conjecture ici est que iOS ne planifie pas les notifications immédiatement, mais seulement à la fin de la boucle d'exécution en cours. Je rencontre ces problèmes lors de la définition de la propriété scheduledLocalNotifications plusieurs fois dans la même boucle d'exécution et les modifications ne semblent pas être mises à jour en conséquence. Je pense que je vais juste garder une copy du tableau des notifications locales moi-même et seulement mettre scheduledLocalNotifications et ne jamais le lire.

Pour iOS8:

Envoyer une notification locale dans 30 secondes à partir de maintenant.

– pour l'objective c

  UILocalNotification *localNotification = [[UILocalNotification alloc] init]; localNotification.alertAction = @"Testing notifications on iOS8"; localNotification.alertBody = @"Woww it works!!"; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow: 30]; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

– pour Swift

  var localNotification:UILocalNotification = UILocalNotification() localNotification.alertAction = "Testing notifications on iOS8" localNotification.alertBody = "Woww it works!! localNotification.fireDate = NSDate(timeIntervalSinceNow: 30) UIApplication.sharedApplication().scheduleLocalNotification(localNotification) 

Enregistrer les parameters de notification

– pour l'objective c

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]] return YES; } 

– pour Swift

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound|UIUserNotificationType.Alert |UIUserNotificationType.Badge, categories: nil) return true } 

Maintenant, vous pouvez également get la notification locale.

Une fois que la notification locale est déclenchée pour une date donnée, les détails n'apparaissent pas dans le tableau [UIApplication sharedApplication] .scheduledLocalNotifications . Donc, si vous donnez un certain nombre d'intervalles à fireDate (de CurrentDate à 10 ~ 20s), il vous montrera les détails.

Je suis confronté à la même question et enfin j'ai obtenu une solution pourquoi c'est arrivé.

si vous n'êtes pas défini fireDate dans l'object de notification. son travail est parfait mais quand vous essayez d'get la list de notification, c'est toujours vide.

voici l'object de notification sans date d'incendie définie

 <UIConcreteLocalNotification: 0x7fc1637aedb0>{fire date = (null), time zone = Asia/Kolkata (GMT+5:30) offset 19800, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Tuesday, September 5, 2017 at 3:31:44 PM India Standard Time, user info = (null)} 

voici l'object de notification avec la date d'incendie

 <UIConcreteLocalNotification: 0x7f96715d0ff0>{fire date = Tuesday, September 5, 2017 at 3:35:17 PM India Standard Time, time zone = Asia/Kolkata (GMT+5:30) offset 19800, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Tuesday, September 5, 2017 at 3:35:17 PM India Standard Time, user info = (null)} 

voici le code que vous pouvez commenter la date d'incendie et vérifier ses différents

  let localNotification = UILocalNotification() **//Comment ..firedate and test it** localNotification.fireDate = NSDate(timeIntervalSinceNow: 5) localNotification.alertBody = "new Blog Posted at iOScreator.com" localNotification.timeZone = NSTimeZone.defaultTimeZone() localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1 UIApplication.sharedApplication().scheduleLocalNotification(localNotification) print(localNotification) for notification in UIApplication.sharedApplication().scheduledLocalNotifications! as [UILocalNotification] { print(notification) }