ios - 设备锁定时警报不工作

标签 ios iphone ipad uilocalnotification

我想在核心位置框架委托(delegate)方法触发时在我的 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 的警报?

最佳答案

你需要告诉手机你的通知有 Action

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

关于ios - 设备锁定时警报不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21531681/

相关文章:

ios - 代码 8 : how to change targeted device family

iphone - 在 iphone 4s 上运行 iphone 应用程序进行测试

android - 在 xmpp 聊天中发送贴纸的最佳方式

iphone - heightForRowAtIndexPath 被调用所有行 & 在性能问题之前 UITableView 中有多少行?

ios - Tableview 标题与单元格重叠

iphone - 应用程序在模拟器上崩溃。适用于 iPhone 设备

iphone - 如何将 uitextfield 设置为斜体 Xcode 4.5

ios - 防止 ios 应用程序关闭

iphone - becomeFirstResponder 问题

iphone - 显示包含数据和控件的表