0

我设置了本地通知。一旦它被触发,假设我的应用程序处于“睡眠”模式,即按下主页按钮或我退出它。现在收到通知。我在我的应用程序上看到红色标记,当我单击该应用程序时,它应该会触发didReceiveLocalNotification,但不会。当我打开我的应用程序时,我怎样才能做到这一点?

4

3 回答 3

1

didReceiveLocalNotification方法仅在您的应用程序在前台运行时调用。如果您看到一个徽章并单击应用程序启动它,那么您需要使用application:willFinishLaunchingWithOptions:(或application:didFinishLaunchingWithOptions:)处理本地通知。要通过这两种方法中的任何一种获取本地通知,请UIApplicationLaunchOptionsLocalNotificationKey用作选项字典的键。

[编辑]来自 UILocalNotification 文档:

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

于 2013-03-20T02:18:13.380 回答
0

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

于 2013-03-20T09:34:42.073 回答
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");
 }

希望对你有帮助.....

于 2013-03-20T04:48:01.597 回答