0

在我的应用程序中,我必须以自定义时间间隔提供通知(文本和声音)。即 1 分钟、2 分钟、3 分钟到 59 分钟,无论我的应用程序处于后台还是应用程序处于活动状态。我为此使用本地通知。

我这里有两个问题:

  1. 当我从我的日期时间选择器中选择任何时间时,我只在 1 分钟内收到了通知。例如。当我选择 5 分钟并启动计时器时,每 1 分钟而不是 5 分钟触发一次通知。如何在自定义时间间隔内获得通知以及如何重复它直到我停止定时器切换。

  2. 我在后台都收到了文字和声音,但是当我的应用程序处于活动状态时,我只有文字没有声音。那么当我的应用程序处于活动状态时如何播放声音。

请给我一些想法。提前致谢。

4

1 回答 1

1
  1. 自定义时间间隔是不可能的
    您不能为 设置自定义时间间隔UILocalNotification,您只能使用NSCalendarUnits为 repeatInterval,例如NSMinuteCalendarUnit

      notification.repeatInterval = NSMinuteCalendarUnit
    
  2. 如果您的应用程序处于前台(活动状态),您需要提供自定义的 alertView 和声音。系统只会调用applicationDidReceiveNotification。为此,您可以使用UIAlertViewAVAudioPlayer

    -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
    
      UIApplicationState state = [[UIApplication sharedApplication] applicationState];
     // checking the state of the application
      if (state == UIApplicationStateActive) 
       {
          // Application is running in the foreground
          // Showing alert
          UIAlertView *alert = [[UIAlertView alloc]initWithTitle:alertTitle message:alertMessage delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitle, nil];
          [alert show];
          [alert release];
    
        //Playing sound
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath],notification.soundName]];
    
        AVAudioPlayer *newAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
        self.audioPlayer = newAudioPlayer;
        self.audioPlayer.numberOfLoops = -1;
        [self.audioPlayer play];
        [newAudioPlayer release];
      }
    }  
    
     - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
      {
        [self.audioPlayer stop];
      }
    
于 2013-05-02T08:47:18.490 回答