IOS 8 [UIApplication sharedApplication] .scheduledLocalNotifications is empty

I am trying to upgrade my application to iOS 8. In the function, I am planning a local notification (I have already verified that firedate and all other parts of the notification are correct) as follows:

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

then I use this code to print the planned local notification:

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

but an array

 [UIApplication sharedApplication].scheduledLocalNotifications 

empty even if the notification is not running. Then, to check if the local notification is really planned, I tried using the code

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

in function

 - (void)applicationWillResignActive:(UIApplication *)application 

of Appdelegate.m in this case, the array of scheduled local notifications is not empty, and the NSLog function displays the correct notification. This only happens on real devices, in the simulator my application works fine. And the problem is not to check the user's permission to schedule local notifications, because I already ran into this. Can someone help me? some ideas?

+6
source share
4 answers

This is not an iOS 8 issue, but

 [UIApplication sharedApplication].scheduledLocalNotifications 

question. The most important answer I found here

Similar problems now. I assume that iOS doesn't schedule notifications right away, but only at the end of the current loop cycle. I am facing these problems when setting up the scheduleLocalNotifications property several times in one run loop and the changes do not seem to be updated accordingly. I think I will just save a copy of the local notification array and set scheduleLocalNotifications and never read it.

+12
source

For iOS8:

Start local notification after 30 seconds.

- for goal 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]; 

- for 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) 

Notification Registration Settings

- for goal c

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

- for Swift

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

Now you can also receive local notification.

+3
source

Once a local notification is fired for a given date, it will not be displayed in the [UIApplication sharedApplication] .scheduledLocalNotifications array . Therefore, if you specify a certain number of intervals on fireDate (from CurrentDate to 10 ~ 20s), it will show you the details.

0
source

I ran into the same problem and finally I got a solution why this happened.

if fireDate is not installed in the notification object. its work is great, but when you try to get a list of notifications, it is always empty.

here the notification object without setting the date by fire

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

here is the object of notifications with the date of fire

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

here is a code that you can comment on the date of fire and check it different

  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) } 
0
source

Source: https://habr.com/ru/post/975574/


All Articles