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.
Legen Sie Ihre vollständige Quellcode hier für '- (void) applicationDidEnterBackground: (UIApplication *) application' und' - (void) Anwendung: (UIApplication *) App didReceiveLocalNotification: (UILocalNotification *) notif ' – viral
Verhalten für' - (void) applicationDidEnterBackground: (UIApplication *) application', das du in deiner Frage erwähnt hast, ist völlig normal. – viral
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