UILocalNotification根本不起作用

我有一些UILocalNotification非常恼人的问题。

在完成我几乎完成的应用程序时,我注意到无论我尝试了什么,都无法获得本地通知。

所以,我不是浪费时间,而是决定回到基础,看看能否让他们工作。

我创build了一个新的XCode基于视图的应用程序,并用-viewDidLoadreplace了-viewDidLoad

 - (void)viewDidLoad { UILocalNotification * theNotification = [[UILocalNotification alloc] init]; theNotification.alertBody = @"Alert text"; theNotification.alertAction = @"Ok"; theNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; [[UIApplication sharedApplication] scheduleLocalNotification:theNotification]; } 

但是,这也没有做任何事情。
我希望在启动应用程序10秒后看到一个通知,但没有出现。
另外,我在iPhone和模拟器上testing了这个。

我在这里错过了一些非常重要的事 ( 我已经通过苹果文档search,找不到为什么会发生这种情况

谢谢

UILocalNotification仅在应用程序运行(或在后台运行)时自动显示。 如果应用程序正在运行并且本地通知被触发,则UIApplicationDelegate- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification方法被调用,系统不显示任何内容(也不会播放声音)。 如果要显示通知,请在委托方法中自己创build一个UIAlertView

只是从我的个人冒险愚蠢的评论…

我有同样的问题,但我的问题是,我忘记了给alertBody分配一个值。 如果您没有设置alertBody,则通知将不会显示。

  1. fireDate必须是未来的时间。
  2. 应用程序必须在背景中运行,或closures。
  3. 还有一件事,不要忘记显示查询是否允许push,将下面的代码添加到AppDelegate中:

     -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil]; [application registerUserNotificationSettings:settings]; } }