Iphone 应用程序打开时未显示本地通知

Iphone 应用程序打开时未显示本地通知,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我已经设置了本地通知。一旦启动,让我们假设我的应用程序处于“睡眠”模式,按下主页按钮,或者我退出它。现在通知已收到。我在我的应用程序上看到红色标签,当我点击应用程序时,它应该会触发direceivelocalnotification,但它不会。当我打开我的应用程序时,我如何让它做到这一点 只有当应用程序在前台运行时才会调用didReceiveLocalNotification方法。如果您看到一个徽章并单击应用程序启动它,那么您需要使用应用程序:willFinishLaunchingWithOpt

我已经设置了本地通知。一旦启动,让我们假设我的应用程序处于“睡眠”模式,按下主页按钮,或者我退出它。现在通知已收到。我在我的应用程序上看到红色标签,当我点击应用程序时,它应该会触发
direceivelocalnotification
,但它不会。当我打开我的应用程序时,我如何让它做到这一点

只有当应用程序在前台运行时才会调用
didReceiveLocalNotification
方法。如果您看到一个徽章并单击应用程序启动它,那么您需要使用
应用程序:willFinishLaunchingWithOptions:
(或
应用程序:didFinishLaunchingWithOptions:
)处理本地通知,以使用这两种方法之一获取本地通知,使用
ui应用程序aunchoptionslocalNotificationKey
作为选项字典的键

来自UILocalNotification文档的[编辑]:

如果本地通知仅标记应用程序图标,则 作为响应,用户启动应用程序 application:didFinishLaunchingWithOptions:方法被调用,但没有 UILocalNotification对象包含在选项字典中

然后实现这个代码

      - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
     // Handle the notificaton when the app is running
      NSDateComponents *comps = [[NSCalendar currentCalendar] components:notif.repeatInterval fromDate:notif.fireDate toDate:[NSDate date] options:0];
     // you can set your own sound 
      NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/win.wav", [[NSBundle mainBundle] resourcePath]]];

     NSError *error;

      UIAlertView *alertView =
     [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"move on", nil)
                           message:[NSString stringWithFormat:@"You have to start %@   meeting within 5 minute",notif.alertBody]
                          delegate:self
                 cancelButtonTitle:nil
                 otherButtonTitles:NSLocalizedString(@"OK", nil), nil];

      notif.soundName = UILocalNotificationDefaultSoundName;


      audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
      audioPlayer.numberOfLoops = -1;
      if (audioPlayer == nil)
         NSLog(@"error%@",[error description]);
      else
         [audioPlayer play];
      [alertView show];
      [alertView release];   

   }


  - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:     (NSInteger)buttonIndex{
     if (buttonIndex==0) {
    [audioPlayer stop];      

     }
    NSLog(@"U HAVE CLICKED BUTTON");
 }

希望这将对您有所帮助。…

当您设置点火日期时,您是否设置了通知对象的userInfo属性???
&在设备或模拟器上检查它???

启动选项返回零。。。。。我可以在通知栏上看到通知,但当我打开应用程序时,启动选项返回nil启动选项字典是否为nil?如果没有,它的内容是什么?是的,那是错误的。它是零。它必须返回一些东西。我试着在后台放一个简单的通知。打开应用程序,按下主页按钮。使用Xcode关闭应用程序,然后在收到通知后,使用Xcode进行反击。仍然为零。[请参见编辑]遗憾的是,我刚刚尝试实现它,我看到了本地通知,但没有看到对应用程序的调用:will。。。对不起,那我该怎么办呢?通知已经发出了。当我点击通知时,它会调用didReceiveNotification。但是,与关闭应用程序时的推送通知不同,它不会显示在DidFinishWithLaunchOptions OK中的launcher选项中。但是您是否设置了通知对象的属性userinfo???@MuhammadUmar&r您在模拟器或真实设备上进行测试我知道通知启动很好…但是要在DidFinshLaunch中接收,您已经在火…这是我问你是否设置了userinfo属性我想在didFinishLaunchOptions中获得它,它与didReceive配合良好。实际上,当应用程序关闭并正常打开时,与推送通知不同的是,它不会出现在启动选项中
      - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
     // Handle the notificaton when the app is running
      NSDateComponents *comps = [[NSCalendar currentCalendar] components:notif.repeatInterval fromDate:notif.fireDate toDate:[NSDate date] options:0];
     // you can set your own sound 
      NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/win.wav", [[NSBundle mainBundle] resourcePath]]];

     NSError *error;

      UIAlertView *alertView =
     [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"move on", nil)
                           message:[NSString stringWithFormat:@"You have to start %@   meeting within 5 minute",notif.alertBody]
                          delegate:self
                 cancelButtonTitle:nil
                 otherButtonTitles:NSLocalizedString(@"OK", nil), nil];

      notif.soundName = UILocalNotificationDefaultSoundName;


      audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
      audioPlayer.numberOfLoops = -1;
      if (audioPlayer == nil)
         NSLog(@"error%@",[error description]);
      else
         [audioPlayer play];
      [alertView show];
      [alertView release];   

   }


  - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:     (NSInteger)buttonIndex{
     if (buttonIndex==0) {
    [audioPlayer stop];      

     }
    NSLog(@"U HAVE CLICKED BUTTON");
 }