UILocalNotification根本不起作用

JPE*_*EG_ 17 xcode objective-c ios uilocalnotification

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

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

所以我没有浪费时间,而是决定回到基础,看看我是否可以让他们工作.

我创建了一个新的基于XCode视图的应用程序,并替换-viewDidLoad为:

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

    [[UIApplication sharedApplication] scheduleLocalNotification:theNotification];
}
Run Code Online (Sandbox Code Playgroud)

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

我错过了一些非常关键的东西吗?(我搜索了Apple文档,找不到任何关于为什么会发生这种情况的信息)

谢谢

mpl*_*ert 43

UILocalNotifications仅在应用程序运行(或在后台运行)时自动显示.如果应用程序正在运行和本地通知火灾UIApplicationDelegate- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification方法被调用,系统不显示任何内容(也没有播放声音).如果要显示通知,请UIAlertView在委托方法中创建自己.


gho*_*ron 16

只是我愚蠢的个人冒险经历的评论......

我有同样的问题,但我的问题是我忘记为alertBody分配一个值.如果未设置alertBody,则不会显示通知.


iba*_*boo 12

  1. fireDate必须是未来的时间.
  2. 应用必须在背景中运行,或者已关闭.
  3. 还有一件事,不要忘记显示是否允许推送查询,将以下代码添加到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]; 
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 没有[Apple文档]中的位置(https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/index.html#//apple_ref/doc/uid/TP40009565-CH1-SW13),我读过我首先需要为只有本地通知的应用程序注册"UserNotificationSettings".这对我很有帮助,听起来很简单. (2认同)