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

时间:2013-03-20 02:07:26

标签: iphone objective-c xcode

我设置了本地通知。一旦它被解雇,让我说我的应用程序处于“睡眠”模式,即按下主页按钮或我退出它。现在收到通知。我在我的应用上看到了红色标记,当我点击该应用时,它应该会触发didReceiveLocalNotification,但事实并非如此。当我打开我的应用程序时,如何才能做到这一点?

3 个答案:

答案 0 :(得分:1)

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

[edit]来自UILocalNotification文档:

  

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

答案 1 :(得分:0)

 -(void)alarmNotification
    {
       NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

      NSDate *newdate = timePicker.date;

      NSLog(@"pickerdate:%@",newdate);

      NSDate *pickerDate =[newdate dateByAddingTimeInterval:-60*1];

      NSLog(@"pickerdate:%@",timePicker.date);

      // Break the date up into components
      NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit |      NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                  fromDate:pickerDate];
      NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                               fromDate:pickerDate];
     // Set up the fire time
      NSDateComponents *dateComps = [[NSDateComponents alloc] init];
     [dateComps setDay:[dateComponents day]];
     [dateComps setMonth:[dateComponents month]];
     [dateComps setYear:[dateComponents year]];
     [dateComps setHour:[timeComponents hour]];
     // Notification will fire in one minute
     [dateComps setMinute:[timeComponents minute]];
     [dateComps setSecond:[timeComponents second]];
      NSDate *itemDate = [calendar dateFromComponents:dateComps];
      [dateComps release];

      localNotif = [[UILocalNotification alloc] init];
      if (localNotif == nil)
         return;
      localNotif.fireDate = itemDate;
       localNotif.timeZone = [NSTimeZone defaultTimeZone];

     // Notification details
      localNotif.alertBody = txtMeetingName.text;

    // Set the action button
      localNotif.alertAction = NSLocalizedString(@"View!", nil);

       localNotif.soundName = UILocalNotificationDefaultSoundName;

       // Specify custom data for the notification
       NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
       localNotif.userInfo = infoDict;

        localNotif.applicationIconBadgeNumber =1;

        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];


      }

然后实现这段代码......

      - (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");
 }
希望这会对你有帮助.....

答案 2 :(得分:0)

当你设置开火日期时,你有设置通知对象的userInfo属性??? &安培;你在设备上还是在模拟器上检查它?