目前市面上很多聚合支付APP都需要在收款成功后,进行语音提示,例如收钱吧,乐惠等!公司App融E收也同样需要实现改功能,主要分为2个部分,一是推送,而是语音播报,下面简单介绍一下
一 推送,目前集成的推送主要是极光推送,集成极光推动的流程比较简单,主要流程是
1.注册账号,在极光推送官网上注册账号,地址:https://www.jiguang.cn/accounts/register/form
2.登录账号,右上角点击创建应用,填写应用名称,上传应用icon,点击创建
3.上传推送证书,做APNS验证
4.导入极光推送SDK, pod 'JPush'
5.在AppDelegete里面导入
// 引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#import <UserNotifications/UserNotifications.h>
做完上面的工作基本已经可以在代码中集成推送了
代码:
//集成推送,注册
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
[JPUSHService setupWithOption:launchOptions appKey:@"You appKey" channel:nil apsForProduction:YES];
//获取registrationID
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
}];
}
#pragma mark - 推送代理方法
//ios10以前调用 --- 前后台收到消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//接收到消息
}
// iOS 10 --- 前台显示推送
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
//接收到消息
}
// iOS 10 --- 后台显示推送
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
//接收到消息
}
// 获取设备deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
[JPUSHService registerDeviceToken:deviceToken];
}
?:当推送做完之后,需要在接收到推送消息之后进行语音播报,这时需要后台(服务器)在集成推送的时候增加一个字段
字段:"content-available" : 0
只有添加这个字段,才能保证在APP在前台运行的时候获取到推送的内容.
二 语音播报
做完推送消息之后,下面就是语音播报了,语音播报这里会有一个坑,当APP退到后台运行或者锁屏状态下时,语音不会播报,但是会走播报的方法,这时我们需要在info.plist里面进行一下权限设置.具体如下图:
或者下图:(比较方便,推荐使用)
这样设置之后,就可以写代码了,主要有2种方法:1.集成第三方SDK(例如:讯飞科大的语音识别);2iOS系统自带的系统方法
首先导入AVFoundation框架
然后需要引入框架到项目中
#import<AVFoundation/AVSpeechSynthesis.h>
1.讯飞科大:
配置流程参考讯飞官网集成流程
网址:http://doc.xfyun.cn/msc_ios/302721
主要代码:
NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@", @"59671829"];
[IFlySpeechUtility createUtility:initString];
//获取语音合成单例
_iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];
//设置协议委托对象
_iFlySpeechSynthesizer.delegate = self;
//设置合成参数
//设置在线工作方式
[_iFlySpeechSynthesizer setParameter:[IFlySpeechConstant TYPE_CLOUD]
forKey:[IFlySpeechConstant ENGINE_TYPE]];
//设置音量,取值范围 0~100
[_iFlySpeechSynthesizer setParameter:@"99"
forKey: [IFlySpeechConstant VOLUME]];
//发音人,默认为”xiaoyan”,可以设置的参数列表可参考“合成发音人列表”
[_iFlySpeechSynthesizer setParameter:@"xiaoyan"
forKey: [IFlySpeechConstant VOICE_NAME]];
//保存合成文件名,如不再需要,设置为nil或者为空表示取消,默认目录位于library/cache下
[_iFlySpeechSynthesizer setParameter:@" tts.pcm"
forKey: [IFlySpeechConstant TTS_AUDIO_PATH]];
// 关闭日志
[IFlySetting showLogcat:NO];
2.系统方法:
在这里对象最好创建为全局的,便于我们控制;当然如果你不需要暂停、继续播放等操作的话可以创建一个局部的。
{
AVSpeechSynthesizer*av;
}
初始化:
//初始化对象
av= [[AVSpeechSynthesizer alloc]init];
av.delegate=self;//挂上代理
AVSpeechSynthesisVoice*voice = [AVSpeechSynthesisVoicevoiceWithLanguage:@"zh-CN"];//设置发音,这是中文普通话
AVSpeechUtterance*utterance = [[AVSpeechUtterance alloc]initWithString:@"需要播报的文字"];//需要转换的文字
utterance.rate=0.6;// 设置语速,范围0-1,注意0最慢,1最快;
utterance.voice= voice;
[avspeakUtterance:utterance];//开始
最后当在后台或锁屏状态下播报:
在AppDelegate的.m文件里面实现下面的方法:
//程序失去焦点
-(void)applicationWillResignActive:(UIApplication* )application
{ //开启后台处理多媒体事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
AVAudioSession *session=[AVAudioSession sharedInstance];
[session setActive:YES error:nil];
//后台播放
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
//这样做,可以在按home键进入后台后 ,播放一段时间,几分钟吧。
//但是不能持续播放网络歌曲,若需要持续播放网络歌曲,还需要申请后台任务id,具体做法是:
//其中的_bgTaskId是后台任务UIBackgroundTaskIdentifier _bgTaskId;
_bgTaskId=[AppDelegate backgroundPlayerID:_bgTaskId];
}
//实现一下backgroundPlayerID:这个方法:
+(UIBackgroundTaskIdentifier)backgroundPlayerID:(UIBackgroundTaskIdentifier)backTaskId
{ //设置并激活音频会话类别
AVAudioSession *session=[AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
//允许应用程序接收远程控制
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
//设置后台任务ID
UIBackgroundTaskIdentifier newTaskId=UIBackgroundTaskInvalid;
newTaskId=[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
if(newTaskId!=UIBackgroundTaskInvalid&&backTaskId!=UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:backTaskId];
}
return newTaskId;
}
到这里就已经可以根据推送消息进行语音播报了,但是苹果审核的时候又一个坑,谁让苹果是爸爸呢!好吧,那就是我们在info.plist里面添加了下面的权限:
想要通过苹果审核,就需要录制一个视屏给苹果审核人员,录制的适配最好上传到YouTube上面(我现在使用的),国外的网站,需要FQ;也可以上传到优酷,但是我没有传过,这样审核就可以通过了.