【发布时间】:2013-05-28 08:17:41
【问题描述】:
在我的 iPhone 应用程序中,我需要显示本地通知。我想知道是否可以设置本地通知在屏幕上显示的时间间隔?例如,我想在 20 秒内显示本地通知。我知道在播放声音时会显示本地通知。但我也发现无法重复本地通知声音:UILocalNotification repeat sound
【问题讨论】:
在我的 iPhone 应用程序中,我需要显示本地通知。我想知道是否可以设置本地通知在屏幕上显示的时间间隔?例如,我想在 20 秒内显示本地通知。我知道在播放声音时会显示本地通知。但我也发现无法重复本地通知声音:UILocalNotification repeat sound
【问题讨论】:
对不起,我的英语不好。 您只能使用 NSCalendarUnit 值重复本地通知。或者重置他(通知)并在旧通知在方法(应用程序:didReceiveLocalNotification :) 中工作时设置新的。为此,您可以将自己的字典添加到通知对象 userInfo 值中,并在以后在此方法中收到通知时处理他。但是,当应用程序未运行或未通过点击通知警报运行时,此方法无法在后台模式下工作。希望这会有所帮助。
【讨论】:
本地通知没有过期的概念。您告诉他们开火,然后由用户决定是否解雇他们或与他们互动。
关于使声音持续 20 秒(除了如果它坚持播放 20 种通知声音我会删除这个应用程序!),假设您当前的声音持续 5 秒,您可以复制粘贴该声音4次进入一个文件,然后播放。我认为最大声音长度为 30 秒,但我找不到支持这一点的文档。
【讨论】:
您可以通过在每 20 秒后更改 notify.firedate 来设置持续时间。
为了保持每 20 秒 - 你可以使用计时器
【讨论】:
如果是重复提示音,可以这样:
UILocalNotification* notifyAlarm =[[UILocalNotification alloc]
init];
if (notifyAlarm)
{
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.fireDate = nil;
notifyAlarm.soundName=@"phone.wav";
notifyAlarm.alertBody =@"value";
notifyAlarm.alertAction=@"value";
notifyAlarm.hasAction=YES;
[[UIApplication sharedApplication] presentLocalNotificationNow:notifyAlarm];
[notifyAlarm release];
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateInactive || state==UIApplicationStateBackground) {
[self backgroundHandlerToPlayCallTone];
}
}
-(void)backgroundHandlerToPlayCallTone {
UIApplication* app = [UIApplication sharedApplication];
bgTaskToPlayCallTone = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
[app endBackgroundTask:self->bgTaskToPlayCallTone];
self->bgTaskToPlayCallTone = UIBackgroundTaskInvalid;
});
}];
// Start the long-running task
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
//dispatch_async(dispatch_get_main_queue(), ^{
[self startCallTone];
[app endBackgroundTask:self->bgTaskToPlayCallTone];
self->bgTaskToPlayCallTone= UIBackgroundTaskInvalid;
});
}
-(void) startCallTone {
if(audioPlayer !=nil) {
[audioPlayer pause];
[audioPlayer release];
audioPlayer=nil;
}
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"phone" ofType:@"wav"]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer setDelegate:self];
audioPlayer.numberOfLoops=10;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
//[[AVAudioSession sharedInstance] setActive:YES withFlags:AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation error:nil];
UInt32 allowMixing = true;
AudioSessionSetProperty (kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(allowMixing),&allowMixing);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer play];
}
【讨论】: