2013-04-08 11 views
9

1分ごとにランダムなローカル通知(テキストとサウンド)が必要です。私は、コードの下に、これを使用しています:バックグラウンドでのランダムローカル通知

self.randomIndex_text = arc4random() % [self.array_motivation count]; 
self.randomIndex_alarm = arc4random() % [self.array_alarm count]; 
NSLog(@"text %d, alarm %d",self.randomIndex_text, self.randomIndex_alarm); 

このコードは完全にこのメソッドの呼び出し以下のコード上で、アラートの[OK]を上から

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif 
{ 
notif.soundName = [NSString stringWithFormat:@"%@.mp3", [self.array_alarm objectAtIndex:self.randomIndex_alarm]]; 
    [self _showAlert:[NSString stringWithFormat:@"%@",[self.array_motivation objectAtIndex:self.randomIndex_text]] withTitle:@"Daily Achiever"]; 
} 

表示、アラートのために働く:

-(void)insert:(NSDate *)fire 
{ 
    self.localNotification = [[UILocalNotification alloc] init]; 

    if (self.localNotification == nil) 
     return; 

    self.randomIndex_text = arc4random() % [self.array_motivation count]; 
    self.randomIndex_alarm = arc4random() % [self.array_alarm count]; 
    NSLog(@"text %d, alarm %d",self.randomIndex_text, self.randomIndex_alarm); 

    self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:refTimeIntrval]; 
    self.localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    self.localNotification.alertBody = [NSString stringWithFormat:@"%@",[self.array_motivation objectAtIndex:self.randomIndex_text]]; 
    self.localNotification.soundName = [NSString stringWithFormat:@"%@.mp3",[self.array_alarm objectAtIndex:self.randomIndex_alarm]]; 
    self.localNotification.alertAction = @"View"; 
    self.localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1; 
    self.localNotification.repeatInterval=NSMinuteCalendarUnit; 

    NSLog(@"alertBody %@,soundName %@", self.localNotification.alertBody, self.localNotification.soundName); 
    [[UIApplication sharedApplication] scheduleLocalNotification:self.localNotification]; 
} 

が、ありませんバックグラウンドでは動作しません。私はちょうど私が一度だけ(バックグラウンドですなわちアプリケーションが動く)でそのapplicationDidEnterBackgroundコールをデバッグないときに私は気づいた

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil); 
    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [application endBackgroundTask:self->bgTask]; 
      self->bgTask = UIBackgroundTaskInvalid; 
     }); 
    }]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     while ([application backgroundTimeRemaining] > 1.0) 
     { 
      UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
      if (localNotif) 
      { 
       self.randomIndex_text = arc4random() % [self.array_motivation count]; 
       self.randomIndex_alarm = arc4random() % [self.array_alarm count]; 
       NSLog(@"tempmethod text %d, alarm %d",self.randomIndex_text, self.randomIndex_alarm); 

       localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:refTimeIntrval]; 
       localNotif.alertBody = [NSString stringWithFormat:@"%@",[self.array_motivation objectAtIndex:self.randomIndex_text]]; 
       localNotif.soundName =[NSString stringWithFormat:@"%@.mp3",[self.array_alarm objectAtIndex:self.randomIndex_alarm]]; 
       localNotif.alertAction = NSLocalizedString(@"Read Msg", nil); 
       localNotif.applicationIconBadgeNumber = 1; 
       [localNotif setRepeatInterval:NSMinuteCalendarUnit]; 
       [application presentLocalNotificationNow:localNotif]; 

       NSLog(@"sound: %@, alertAction: %@, alerBody: %@, ref: %f, str_time: %@",localNotif.soundName, localNotif.alertAction, localNotif.alertBody, refTimeIntrval, str_Time); 

       [self performSelector:@selector(bgmethodd) withObject:nil afterDelay:refTimeIntrval]; 
       break; 
      } 
     } 
     [application endBackgroundTask:self->bgTask]; 
     self->bgTask = UIBackgroundTaskInvalid; 
    }); 
    NSLog(@"smh: %d,%d,%d",self.seconds, self.minutes, self.hours); 
    } 
} 

もう一つのランダムな方法の上にこれを置きます。その後、アプリケーションが再び開かれるまで何もメソッドコールを呼び出すことはできませんが、それでも私は通知テキストとサウンドを得ることができます。しかし、このテキストとサウンドはランダムではありません。

私はいくつかのアイデアをお伝えし、バックグラウンドでメソッド呼び出しがない場合に、この通知テキストとサウンドがどこに来るかを知ってください。また、バックグラウンドで通知をランダムに行うことも可能です。 ありがとうございます。

+0

'のためにここにあなたの完全なソースコードを入れてください - (空)applicationDidEnterBackground: - 'のため ' – viral

+0

行動NOTIF(UILocalNotification *) - ::(のUIApplication *)アプリのdidReceiveLocalNotification(無効)アプリケーション(のUIApplication *)application'と' (void)applicationDidEnterBackground:(UIApplication *)application'、あなたの質問で言及したのは完全に正常です。 – viral

+0

バックグラウンドでランダム通知を受け取ることはできますか?そして私はこのリンクと同じコードを使用しています:iphonesdkdev.blogspot.in/2010/04/local-push-notification-sample-code-os.html – Abha

答えて

2

アプリがバックグラウンドで実行されるときに最初に通知する通知は、間隔NSMinuteCalendarUnitで繰り返されるため、アプリは1分ごとにその通知のみを表示します。

ランダムなアラートとサウンドを取得するには、ローカルの通知で、バックグラウンドで次のランダムなサウンドとアラートを生成するコードを実行する必要がありますが、これは不可能です。

これを行う1つの方法は、事前にランダムなサウンドとアラートで64(最大)のローカル通知をスケジュールすることです。ユーザーがアプリを開くと、バックグラウンドで発生した通知の数を確認し、通知を再スケジュールすることができます。

これらの64の通知中にユーザーがアプリケーションを開いていなくてもローカルの通知が発生するようにするには、最後の通知を間隔NSMinuteCalendarUnitで繰り返す必要があります。だから、最初の63回の通知の後、あなたはランダム性を失うでしょうが、ユーザーが頻繁にアプリを開くと、これは問題にはなりません。

関連する問題