2013-04-08 9 views
9

Ich möchte zufällige lokale Benachrichtigung (Text und Ton) in jeder Minute. Ich bin mit diesem Code unten:Zufällige lokale Benachrichtigung im Hintergrund

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); 

Dieser Code funktioniert perfekt für

- (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"]; 
} 

Anzeige Alarm von oben Code und auf Ok Alarm dies unter Methodenaufruf:

-(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]; 
} 

aber tut nicht im Hintergrund arbeiten. Ich habe dies nur über Zufallsverfahren in

- (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); 
    } 
} 

Eine weitere Sache, bemerkte ich, wenn ich debuggen Sie, dass applicationDidEnterBackground Anruf zu einem Zeitpunkt nur (das heißt, wenn die Anwendung bewegt sich im Hintergrund). Danach wurde keine Methode aufgerufen, bis die Anwendung wieder geöffnet wurde, aber ich bekam immer noch Benachrichtigungstext und Ton. Aber dieser Text und Ton ist nicht zufällig.

Bitte schlagen Sie mir eine Idee vor und teilen Sie Ihr Wissen, dass von wo diese Benachrichtigung Text und Sound kommen, wenn keine Methode Aufruf im Hintergrund. Und ist es möglich, eine Benachrichtigung im Hintergrund zufällig zu machen? Vielen Dank im Voraus.

+0

Legen Sie Ihre vollständige Quellcode hier für '- (void) applicationDidEnterBackground: (UIApplication *) application' und' - (void) Anwendung: (UIApplication *) App didReceiveLocalNotification: (UILocalNotification *) notif ' – viral

+0

Verhalten für' - (void) applicationDidEnterBackground: (UIApplication *) application', das du in deiner Frage erwähnt hast, ist völlig normal. – viral

+0

Ist es möglich, eine zufällige Benachrichtigung im Hintergrund zu erhalten? Und ich benutze den gleichen Code wie unter dem folgenden Link: iphones.docdev.blogspot.in/2010/04/local-push-notification-sample-code-os.html – Abha

Antwort

2

Die erste Benachrichtigung, die Sie planen, wenn die App im Hintergrund ausgeführt wird, wiederholt sich mit dem Intervall NSMinuteCalendarUnit und daher zeigt die App nur jede Minute diese Benachrichtigung an.

Um zufällige Alarme und Töne zu erhalten, muss die lokale Benachrichtigung einen Code im Hintergrund ausführen, der den nächsten zufälligen Ton und Alarm erzeugt, was nicht möglich ist.

Eine Möglichkeit, dies zu tun, besteht darin, im Voraus 64 (max.) Lokale Benachrichtigungen mit zufälligen Tönen und Warnungen zu planen. Wenn der Benutzer die App öffnet, können Sie sehen, wie viele Benachrichtigungen im Hintergrund ausgelöst wurden, und sie neu planen.

Um sicherzustellen, dass lokale Benachrichtigungen ausgelöst werden, selbst wenn der Benutzer die Anwendung während dieser 64 Benachrichtigungen nicht öffnet, muss die letzte Benachrichtigung mit dem Intervall NSMinuteCalendarUnit wiederholt werden. Nach den ersten 63 Benachrichtigungen werden Sie die Zufälligkeit verlieren, aber wenn der Benutzer die App häufig öffnet, wird dies kein Problem sein.

Verwandte Themen