设备锁定时报警无效

时间:2014-02-03 15:49:28

标签: ios iphone ipad uilocalnotification

当核心位置框架委托方法触发时,我想在我的IOS设备中触发警报。当应用程序位于前台时,警报会正确响铃。但是当设备锁定时,警报功能不起作用。这是我用过的代码。

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date];

localNotification.alertBody = @"body";
localNotification.soundName = @"RING.WAV";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

是因为我使用过UILocalNotification类吗?如果是这样,是否有替代方案。

在锁定屏幕中,显示本地通知警报正文,但警报不响。

编辑:

在appdelegate中,我有代表接收本地通知

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/RING.WAV", [[NSBundle mainBundle] resourcePath]]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;

if (audioPlayer == nil)
    NSLog(@"error :%@",[error description]);
else 
    [audioPlayer play];
}

我发现当设备被锁定时不会触发此方法。当设备被锁定时,有没有办法根据UILocalNotification触发警报?

2 个答案:

答案 0 :(得分:2)

您需要告诉手机您的通知有行动

[localNotification setAlertAction:@"My Action"];
[localNotification setHasAction:YES];

答案 1 :(得分:0)

在这种情况下,设置音频会话属性就可以了。

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error: nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
[AVAudioSession sharedInstance] setActive: YES error: nil];