我想在每分鐘隨機本地通知(文本和聲音)。我用下面這段代碼:後臺隨機本地通知
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);
該代碼完全適用於
- (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"];
}
顯示警報從上面的代碼,並就OK了戒備這下面的方法調用:
-(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];
}
但確實不在後臺工作。我只是把這個上面隨機方法
- (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);
}
}
還有一兩件事,我注意到當我做調試applicationDidEnterBackground
調用只在一個時間(即當在後臺應用程序移動)。之後,沒有任何方法調用,直到應用程序再次打開,但我仍然得到通知文本和聲音連續性。但是這個文本和聲音不是隨機的。
請建議我一些想法,並分享你的知識,當這個通知文本和聲音來自何處,當沒有任何方法調用背景。並且可以在後臺隨機發出通知。 在此先感謝。
把你的全部源代碼這裏' - (空)applicationDidEnterBackground:(UIApplication的*)application'和' - (空)應用程序:(UIApplication的*)應用didReceiveLocalNotification:(UILocalNotification *)通知符 ' – viral 2013-04-08 09:10:22
行爲的' - (void)applicationDidEnterBackground:(UIApplication *)application',你在你的問題中提到的是非常正常的。 – viral 2013-04-08 09:11:17
是否有可能在後臺獲得隨機通知?我使用下面的鏈接代碼相同:iphonesdkdev.blogspot.in/2010/04/local-push-notification-sample-code-os.html – Abha 2013-04-08 09:18:16