如何设置本地通知的持续时间



在我的iPhone应用程序,我需要显示本地通知。我想知道是否可以为屏幕上显示的本地通知设置时间间隔?例如,我想在20秒内显示本地通知。我知道在播放声音的时候会显示本地通知。但我也发现,这是不可能重复本地通知声音:UILocalNotification重复声音

对不起,我的英语不好。你只能使用NSCalendarUnit值来重复本地通知。或者当旧的通知在方法(application:didReceiveLocalNotification:)中工作时,重置他(notification)并设置new。为此,您可以将自己的字典添加到通知对象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];
}

最新更新