iOS 8 .scheduledLocalNotifications为空

我试图更新我的应用程序到iOS 8.在一个函数中,我安排了一个本地通知(我已经检查过,通知的所有其他部分是正确的)以这种方式:

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

那么我使用这个代码打印预定的本地通知:

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

但arrays

 [UIApplication sharedApplication].scheduledLocalNotifications 

即使通知未被触发,也是空的。 然后,为了检查本地通知是否真的计划好了,我试着使用这个代码

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

在函数中

 - (void)applicationWillResignActive:(UIApplication *)application 

在这种情况下, Appdelegate.m的计划本地通知数组不是空的,NSLog函数打印正确的通知。 这只发生在真实的设备上,在模拟器我的应用程序工作正常。 问题是不检查用户的权限来安排本地通知,因为我已经面对它。 有人可以帮我吗? 一些想法?

这不是iOS 8的问题,但是

 [UIApplication sharedApplication].scheduledLocalNotifications 

问题。 最相关的答案,我在这里find

现在有类似的问题。 我的猜测是,iOS不会立即安排通知,而只是在当前运行循环的最后。 我在同一个运行循环中多次设置scheduledLocalNotifications属性时遇到了这些问题,并且更改似乎没有相应更新。 我想我会保留本地通知数组的副本,只设置scheduledLocalNotifications并从不读取它。

对于iOS8:

从现在起30秒内发出本地通知。

– 对于目标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]; 

– 为斯威夫特

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

注册通知设置

– 对于目标c

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

– 为斯威夫特

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

现在您也可以获得本地通知。

一旦本地通知在指定的date被触发,它将不会在[UIApplication sharedApplication] .scheduledLocalNotifications数组中显示详细信息。 所以,如果你给fireDate(从CurrentDate到10〜20s)的时间间隔计数,它会告诉你的细节。

我面临着同样的问题,最后我得到了解决scheme为什么发生了。

如果你没有在通知对象中设置fireDate 。 它的工作是完美的,但是当你尝试获得通知列表时,它总是空白。

这里是通知对象,没有设置启动date

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

这里是通知对象与火灾date

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

这里是代码,你可以评论火灾date,并检查其不同

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