iOS12 APNS语音播报在NotificationService扩展中无法播报。app在后台
iOS 12.1 NotificationService不走,在我的工程里直接跑不起来,我怀疑是某个C++的库导致。如果是后台播报,可以在AppDelegate中申请后台任务,开启后台播放,NotificationService的target开启后台播放模式。
- (void)applicationWillResignActive:(UIApplication *)application {
//开启后台处理多媒体事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
//后台播放
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
//申请后台任务id,程序长时间处于后台也可播放音频
self.bgTaskId = [AppDelegate backgroundPlayerID:self.bgTaskId];
}
并且在以下方法中区分下系统版本,还是可以实现前、后台、锁屏播报,但是杀进程播报还是解决不了- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSString *msg = LEmpty(userInfo[@"aps"][@"alert"]);
//NotificationServiceiOS 10以上使用,但是在12.1及以上不会执行//iOS 10以下、iOS 12.1以上直接播放if (!iOS10_OR_LATER || ([[[UIDevice currentDevice] systemVersion] compare:@"12.1" options:NSNumericSearch] != NSOrderedAscending)) {
//语音播报[[VoiceBroadcastManager sharedInstance] playVoice:msg];} else {
//iOS 10以上 ~ iOS 12.1以下 走NotificationService}
iOS 12.1 NotificationService不走,在我的工程里直接跑不起来,我怀疑是某个C++的库导致。如果是后台播报,可以在AppDelegate中申请后台任务,开启后台播放,NotificationService的target开启后台播放模式。
- (void)applicationWillResignActive:(UIApplication *)application {
//开启后台处理多媒体事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
//后台播放
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
//申请后台任务id,程序长时间处于后台也可播放音频
self.bgTaskId = [AppDelegate backgroundPlayerID:self.bgTaskId];
}
并且在以下方法中区分下系统版本,还是可以实现前、后台、锁屏播报,但是杀进程播报还是解决不了
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSString *msg = LEmpty(userInfo[@"aps"][@"alert"]);
//NotificationServiceiOS 10以上使用,但是在12.1及以上不会执行
//iOS 10以下、iOS 12.1以上直接播放
if (!iOS10_OR_LATER || ([[[UIDevice currentDevice] systemVersion] compare:@"12.1" options:NSNumericSearch] != NSOrderedAscending)) {
//语音播报
[[VoiceBroadcastManager sharedInstance] playVoice:msg];
} else {
//iOS 10以上 ~ iOS 12.1以下 走NotificationService
}