后台推送-语音播报

语音播报功能的实现必须是推送语音合成,选择的推送是极光推送,本文最终实现的效果即使APP被杀死也可以进行语音播报

配置推送证书,极光文档里面有,把极光推送集成进去就不说了

语音合成我用的是系统的方法,不过语音死板不好听,但是使用很简单,3行代码就可以,建议使用其他的SDK

AVSpeechUtterance*utterance = [AVSpeechUtterancespeechUtteranceWithString:@"成功集成语音播报"];

AVSpeechSynthesizer*synth = [[AVSpeechSynthesizeralloc] init];

[synth speakUtterance:utterance];

在收到通知的时候使用上面的3行代码就可以进行语音播报,但是只限于APP前台运行,当后台运行的时候语音播报便不可以了,此时需要加入下面代码让语音播报可以在后台运行,但是杀死的情况下不能播报,杀死重新启动返回后台也不可以播报. 我是在AppDelegate里面写入这个方法的

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions;

把下面的代码写入进去

NSError*error =NULL;

AVAudioSession*session = [AVAudioSessionsharedInstance];

[session setCategory:AVAudioSessionCategoryPlaybackerror:&error];

if(error) {

// Do some error handling

}

[session setActive:YESerror:&error];

if(error) {

// Do some error handling

}

// 让app支持接受远程控制事件

[[UIApplicationsharedApplication] beginReceivingRemoteControlEvents];

让语音播报在后台也可以进行的话就需要在

// 在AppDelegate定义属性

@property(nonatomic,unsafe_unretained)UIBackgroundTaskIdentifierbackgroundTaskIdentifier;

- (void)applicationWillResignActive:(UIApplication*)application;

里面加入以下的方法

- (void)applicationWillResignActive:(UIApplication*)application {

// 开启后台处理多媒体事件

[[UIApplicationsharedApplication] beginReceivingRemoteControlEvents];

AVAudioSession*session=[AVAudioSessionsharedInstance];

[session setActive:YESerror:nil];

// 后台播放

[session setCategory:AVAudioSessionCategoryPlaybackerror:nil];

// 这样做,可以在按home键进入后台后 ,播放一段时间,几分钟吧。但是不能持续播放网络歌曲,若需要持续播放网络歌曲,还需要申请后台任务id,具体做法是:

_backgroundTaskIdentifier=[AppDelegate backgroundPlayerID:_backgroundTaskIdentifier];

// 其中的_bgTaskId是后台任务UIBackgroundTaskIdentifier _bgTaskId;

}

//实现一下backgroundPlayerID:这个方法:

+(UIBackgroundTaskIdentifier)backgroundPlayerID:(UIBackgroundTaskIdentifier)backTaskId{

//设置并激活音频会话类别

AVAudioSession*session=[AVAudioSessionsharedInstance];

[session setCategory:AVAudioSessionCategoryPlaybackerror:nil];

[session setActive:YESerror:nil];

//允许应用程序接收远程控制

[[UIApplicationsharedApplication] beginReceivingRemoteControlEvents];

//设置后台任务ID

UIBackgroundTaskIdentifiernewTaskId=UIBackgroundTaskInvalid;

newTaskId=[[UIApplicationsharedApplication] beginBackgroundTaskWithExpirationHandler:nil];

if(newTaskId!=UIBackgroundTaskInvalid&&backTaskId!=UIBackgroundTaskInvalid){

[[UIApplicationsharedApplication] endBackgroundTask:backTaskId];

}

returnnewTaskId;

}

到这里就可以进行后台播报了,但是注意到这一步只有在程序没有被杀死的情况下才可以播报, 杀死之后是不能播报的, 所有我们还要进行处理,这时需要使用 UNNotificationServiceExtension.

创建 UNNotificationServiceExtension

后台推送-语音播报_第1张图片

填写文件名,边创建好了

后台推送-语音播报_第2张图片

NotificationService.m里面有一个方法

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void(^)(UNNotificationContent * _Nonnull))contentHandler;

此时把语音播报写进去就可以了

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void(^)(UNNotificationContent * _Nonnull))contentHandler {

self.contentHandler = contentHandler;

self.bestAttemptContent = [request.content mutableCopy];

// Modify the notification content here...

self.bestAttemptContent.title = [NSStringstringWithFormat:@"%@ [modified]",self.bestAttemptContent.title];

NSString*content = request.content.userInfo[@"aps"][@"alert"][@"body"];

AVSpeechUtterance*utterance = [AVSpeechUtterancespeechUtteranceWithString:content];

AVSpeechSynthesizer*synth = [[AVSpeechSynthesizeralloc] init];

[synth speakUtterance:utterance];

self.contentHandler(self.bestAttemptContent);

}

认为到这里就完成了?,NO!在发送推送的时候还需要在极光推送服务里面配置一下

后台推送-语音播报_第3张图片

到这一步的时候,后台播报就可以执行了, 但是此播报服务只能在 iOS10 系统之后才可以进行, 如果想适配iOS9之前的只能做一个固定的音频文件放到项目里面,比如支付宝的的到账提醒, 然后在推送的时候

后台推送-语音播报_第4张图片

这时候就可以完美播放音频文件了, 提醒:如果不需要动态的语音播放, 直接可以使用这个方法,不需要配置 UNNotificationServiceExtension 和后台播放了,因为这个方法是系统认为推送的提示音

你可能感兴趣的:(后台推送-语音播报)