Abh*_*bha 9 iphone ios uilocalnotification
我希望每分钟都有随机的本地通知(文本和声音).我在下面使用以下代码:
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);
Run Code Online (Sandbox Code Playgroud)
这段代码完美适用
- (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"];
}
Run Code Online (Sandbox Code Playgroud)
从上面的代码中显示警报,并在以下方法调用上提示:
-(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];
}
Run Code Online (Sandbox Code Playgroud)
但在背景中不起作用.我把这个随机方法放在上面
- (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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我applicationDidEnterBackground仅在一次调试该调用时(即当应用程序在后台运行时),我注意到了另外一件事.之后没有任何方法调用,直到应用程序再次打开,但仍然有通知文本和声音连续.但是这个文字和声音并不是随机的.
请建议我一些想法,并分享您的知识,从背景中没有任何方法调用时通知文本和声音的来源.是否可以在后台随机发出通知.提前致谢.
当应用程序进入后台时您安排的第一个通知将以 NSMinuteCalendarUnit 间隔重复,因此应用程序每分钟仅显示该通知。
为了获得随机警报和声音,本地通知需要在后台执行一些代码来生成下一个随机声音和警报,这是不可能的。
实现此目的的一种方法是提前安排 64 个(最多)本地通知以及随机声音和警报。当用户打开应用程序时,您可以看到在后台触发了多少通知,并重新安排它们。
为了确保即使用户在这 64 个通知期间没有打开应用程序也会触发本地通知,最后一个通知需要以 NSMinuteCalendarUnit 间隔重复。因此,在前 63 个通知之后,您将失去随机性,但如果用户频繁打开应用程序,这不会成为问题。
| 归档时间: |
|
| 查看次数: |
597 次 |
| 最近记录: |