Chcę losowe lokalne powiadomienie (tekst i dźwięk) w każdej minucie. Używam tego poniżej kodu:Losowe lokalne powiadomienie w tle
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);
Kod ten doskonale działa na pogotowiu
- (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"];
}
wyświetlacza z góry kod i OK ostrzegania tym poniżej metody połączenia:
-(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];
}
ale robi nie działa w tle. Ja po prostu umieścić to powyżej metody losowego w
- (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);
}
}
jeszcze jedno zauważyłem kiedy robię debugowania że applicationDidEnterBackground
rozmowy tylko w jednym czasie (czyli gdy porusza aplikacji w tle). Potem żadna metoda nie zadzwoni, dopóki aplikacja się nie otworzy, ale wciąż mam komunikat z powiadomieniem i dźwięk ciągły. Ale ten tekst i dźwięk nie są przypadkowe.
Proszę zasugerować mi jakiś pomysł i podzielić się wiedzą, że skąd pochodzi ten tekst i dźwięk powiadomienia, gdy żadna metoda nie wywoła w tle. I czy można dokonać losowego powiadomienia w tle. Z góry dzięki.
Połóż pełny kod źródłowy tutaj '- (void) applicationDidEnterBackground: (*) rolnicze wykorzystanie UIApplication i' - (void) Zastosowanie: (UIApplication *) app didReceiveLocalNotification: (UILocalNotification *) notif ' – viral
zachowanie dla' - (void) applicationDidEnterBackground: Aplikacja (UIApplication *) ", o której wspomniałeś w swoim pytaniu, jest zupełnie normalna. – viral
Czy można uzyskać losowe powiadomienie w tle? Używam tego samego kodu, co poniższy link: iphonesdkdev.blogspot.in/2010/04/local-push-notification-sample-code-os.html – Abha