0

私のアプリでは、カスタムの時間間隔で通知 (テキストとサウンド) を提供する必要があります。つまり、1 分、2 分、3 分、最大 59 分まで、アプリがバックグラウンドにあるか、アプリがアクティブになっています。これにはローカル通知を使用しています。

ここには2つの問題があります:

  1. 日時ピッカーからいつでも選択すると、わずか1分で通知が届きました。例えば。5分を選択してタイマーを開始すると、5分ではなく1分ごとに通知が発生します。カスタムの時間間隔で通知を受け取る方法と、タイマー スイッチを停止するまで通知を繰り返す方法を教えてください。

  2. バックグラウンドでテキストとサウンドの両方を取得しましたが、アプリがアクティブなときは、サウンドではなくテキストのみを取得しました。では、アプリがアクティブなときにサウンドを再生するにはどうすればよいですか。

何かアイデアを提案してください。前もって感謝します。

4

1 に答える 1

1
  1. カスタム時間間隔は
    使用 できません のカスタム時間間隔を設定することはできません。次のような repeatInterval には NSCalendarUnitsのみUILocalNotificationを使用できます。NSMinuteCalendarUnit

      notification.repeatInterval = NSMinuteCalendarUnit
    
  2. アプリがフォアグラウンド (アクティブ) にある場合は、カスタムの alertView とサウンドを提供する必要があります。システムは呼び出しapplicationDidReceiveNotificationのみを行います。そのためにあなたが使用することができUIAlertViewますAVAudioPlayer

    -(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 に答える