项目需求:App在前台与后台运行收到消息进行语音播报消息内容
极光满足条件App在前台运行时候,收到推送消息进行播报(系统语音,讯飞,百度语音)都可以实现,App在后台挂起时下面截图方法是不调用的。

这两个方法只有在APP前台运行与点击通知栏才会触发,无法满足项目需求
iOS10之后的通知具有通知扩展功能,可以在系统受到通知、展示通知时做一些事情 UNNotificationServiceExtension:通知服务扩展,是在收到通知后,展示通知前,做一些事情的。
1.创建一个UNNotificationServiceExtension(Content Extension是定制通知栏的扩展)

2.如图创建完可以看到工程中多出一个文件

3.在NotificationService.m文件中,有两个自动生成的方法:
// 系统接到通知后,有最多30秒在这里重写通知内容(在此方法可进行一些网络请求,如上报是否收到通知等操作)-(void)didReceiveNotificationRequest:(UNNotificationRequest*)request withContentHandler:(void(^)(UNNotificationContent*contentToDeliver))contentHandler;// 处理过程超时,则收到的通知直接展示出来-(void)serviceExtensionTimeWillExpire;
可以重写一些属性:
self.bestAttemptContent.title = @"我是大标题";
self.bestAttemptContent.subtitle = @"我是小标题";
self.bestAttemptContent.body =@"我是内容";
self.bestAttemptContent.sound = @"可以获取本地音频";
4.通知扩展是一个Target,需要在主工程把BackgroundModes里支持项打开。


5.运行项目先运行扩展Target在运行主工程,断点测试需要当前Target为通知扩展
6.在极光后台需要配置content-available,mutable-content 不然不会走通知扩展
现在就可以在扩展里实现语音播报功能了,原生语音合成、百度、讯飞这些语音都不可以播报,苹果在12.1版本以后取消了扩展的播放权限,这次实现是用本地音频来实现的,提前录制好:0~9,十,百,千,万,元,支付宝付款,微信付款,18个音频文件导入xcode中。
主要实现原理:通过信号量用在多线程并发的,一个线程完成了某一个动作就通过信号量告诉别的线程,别的线程再进行某些动作
这里提前准备好的音频:支付宝付款,微信付款文字较长,在信号量执行过程中加了延迟处理。

下面的方法是对本地音频处理的过程

在拿到推送过来的消息以后要对金额进行算法的处理,这里是通过swift来实现的算法,oc桥接来进行的处理


算法搞定之后,会拿到一个字符串,把字符串进行拆分,存到数组里,通过和本地音频字符串判断 进行语音播报。

网友评论