iOS开发 - NotificationService语音播报

iOS NotificationService语音播报

最近碰到个接收到推送要实现语音播报的需求,需要后台推送通知,APP客户端收到通知之后语音播放:“您的账户收到一笔巨款”的功能。

因为工程之前已经集成了极光推送服务。这里直接使用Notification Service Extension通知服务扩展

Notification Service Extension 简单说我们在收到推送且在弹框展示之前,对通知内容进行修改(只针对远程推送)。

一 主工程需要如下配置:

设置Background Modes

在这里插入图片描述

二 IOS 10 创建Notification Service Extension

创建Notification Service Extension的target

在这里插入图片描述

创建后系统会生成NotificationService.h和NotificationService.m两个文件。

设置下Targets中的NotificationService的 Push Notifications

在这里插入图片描述

三 使用AVSpeechUtterance

在NotificationService中代码


#import "NotificationService.h"
#import <AVFoundation/AVFAudio.h>
#import <AVFoundation/AVFoundation.h>@interface NotificationService ()<AVSpeechSynthesizerDelegate>@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;@property (nonatomic, strong) AVSpeechSynthesisVoice *synthesisVoice;
@property (nonatomic, strong) AVSpeechSynthesizer *synthesizer;@end@implementation NotificationService- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {self.contentHandler = contentHandler;self.bestAttemptContent = [request.content mutableCopy];// 这个info 内容就是通知信息携带的数据,后面我们取语音播报的文案,通知栏的title,以及通知内容都是从这个info字段中获取NSString *type = self.bestAttemptContent.userInfo[@"type"];NSString *typeStr = [NSString stringWithFormat:@"%@",type];// 语音播报if (7 == typeStr.integerValue) {// 类型为7NSString *info = self.bestAttemptContent.userInfo[@"content"];// 播报语音[self playVoiceWithContent:info];}// 这行代码需要注释,当我们想解决当同时推送了多条消息,这时我们想多条消息一条一条的挨个播报,我们就需要将此行代码注释//    self.contentHandler(self.bestAttemptContent);
}- (void)playVoiceWithContent:(NSString *)content {if (!(content && [content isKindOfClass:[NSString class]] && content.length > 0)) {return;}AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:content];utterance.rate = 0.5;utterance.voice = self.synthesisVoice;[self.synthesizer speakUtterance:utterance];
}// 新增语音播放代理函数,在语音播报完成的代理函数中,我们添加下面的一行代码
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {    // 每一条语音播放完成后,我们调用此代码,用来呼出通知栏self.contentHandler(self.bestAttemptContent);
}- (void)serviceExtensionTimeWillExpire {// Called just before the extension will be terminated by the system.// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.self.contentHandler(self.bestAttemptContent);
}// 调试,代替debug
- (void)playVoice:(NSString *)info {AVSpeechSynthesizer *av = [[AVSpeechSynthesizer alloc] init];AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:info];utterance.rate = 0.5;utterance.voice= voice;[av speakUtterance:utterance];
}- (AVSpeechSynthesisVoice *)synthesisVoice {if (!_synthesisVoice) {_synthesisVoice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];}return _synthesisVoice;
}- (AVSpeechSynthesizer *)synthesizer {if (!_synthesizer) {_synthesizer = [[AVSpeechSynthesizer alloc] init];_synthesizer.delegate = self;}return _synthesizer;
}@end

四 IOS 10之前版本在AppDelegate中的设置


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {DebugLog(@"收到通知:%@", userInfo);[[DFAVSpeechManager sharedInstance] playAVSpeechUserInfo:userInfo completion:nil];completionHandler(UIBackgroundFetchResultNewData);
}

五 IOS 设置app打开的时候在前台的推送栏显示

如果是语音播报,这里我设置需要显示推送栏。


#pragma mark - IOS10 LaterNotification UNUserNotificationCenterDelegate Methods
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{DebugLog(@"willPresentNotification");NSDictionary * userInfo = notification.request.content.userInfo;UNNotificationRequest *request = notification.request; // 收到推送的请求UNNotificationContent *content = request.content; // 收到推送的消息内容NSNumber *badge = content.badge;  // 推送消息的角标NSString *body = content.body;    // 推送消息体UNNotificationSound *sound = content.sound;  // 推送消息的声音NSString *subtitle = content.subtitle;  // 推送消息的副标题NSString *title = content.title;  // 推送消息的标题if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {DebugLog(@"iOS10 前台收到远程通知:%@",userInfo);} else {// 判断为本地通知DebugLog(@"iOS10 前台收到本地通知:{nbody:%@,ntitle:%@,nsubtitle:%@,nbadge:%@,nsound:%@,nuserInfo:%@n}",body,title,subtitle,badge,sound,userInfo);}BOOL needShowPushColum = [[DFAVSpeechManager sharedInstance] canShowPushColumn:userInfo];if (needShowPushColum) {//前台接到推送展示 则需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);} else {//前台接到推送不展示completionHandler(0);}
}

六 在极光后台设置进行推送

在极光后台设置进行推送

当然特别需要注意的地方是如果服务器推送语音播报,需要设置mutable-content为1 或 true

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/21765.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue3+Vite+Pinia+Naive后台管理系统搭建之八:构建 login.vue 登录页

前言 如果对 vue3 的语法不熟悉的&#xff0c;可以移步Vue3.0 基础入门&#xff0c;快速入门。 项目所需要的图片&#xff0c;icon图标&#xff08;推荐&#xff1a;阿里巴巴矢量图标库&#xff09;自行获取&#xff0c;命名一致就行。 1. 构建 src/components/CopyRight.vu…

MySQL~索引的优缺点是什么?有哪些优化索引的方法?

1.索引的优缺点 优点&#xff1a;提高查询记录的速度。 缺点&#xff1a; 需要占用空间&#xff0c;索引是一种用空间换时间的做法创建索引和维护索引都需要消耗时间&#xff0c;会降低表的增删查改效率&#xff0c;因为每次进行增删查改&#xff0c;都需要对索引进行维护&a…

系统设计蓝图 / 备忘单

开发一个强大、可扩展和高效的系统可能会令人望而却步。然而&#xff0c;了解关键概念和组件可以使这个过程更可管理。在本博客文章中&#xff0c;我们将探讨系统设计的关键概念和组件&#xff0c;如DNS、负载均衡、API网关等&#xff0c;以及一个简明的备忘单&#xff0c;可以…

自定义类型详解

目录 一、结构体类型 1.认识结构体 2.如何使用结构体类型 2.1按顺序初始化结构体&#xff0c;并通过.符号访问 ​2.2无序初始化结构体&#xff0c;并用.符号访问 2.3通过地址的方式访问结构体变量成员 2.4结构体的自引用 2.4.1错误的自引用 2.4.2正确的自引用 3.结构体内存…

Ansible 自动化运维工具(完善版)

目录 Ansible概述 Ansible特点 Ansible应用 1、使用者 2、Ansible工具集合 3、作用对象 Ansible的搭建 环境 ansible主机 1、ansible 2、Ansible-doc Ansible模块 1.command模块 2.shell模块 3.raw模块 Ansible概述 Ansible是最近非常火的一款开源运维自动化工具…

最全的ubuntu20.04上安装nvidia和cuda

文章目录 一、环境要求二、查询推荐安装的驱动版本三、安装470四、查看显卡驱动是否成功五、安装对应版本的cuda 官方路径 一、环境要求 ubuntu20.04如果之前有过驱动应该先卸载 sudo apt-get --purge remove nvidia* sudo apt autoremove# 卸载cuda sudo apt-get --purge r…

Oracle通过函数调用dblink同步表数据方案(全量/增量)

创建对应的包&#xff0c;以方便触发调用 /*包声明*/ CREATE OR REPLACE PACKAGE yjb.pkg_scene_job AS /*创建同步任务*/FUNCTION F_SYNC_DRUG_STOCK RETURN NUMBER;/*同步*/PROCEDURE PRC_SYNC_DRUG_STOCK(RUNJOB VARCHAR2) ; END pkg_scene_job; /*包体*/ CREATE OR REPL…

Hudi基础知识讲解

Hudi概述 Hudi是一种数据湖的存储格式&#xff0c;在Hadoop文件系统之上提供了更新数据和删除数据的能力以及消费变化数据的能力。支持多种计算引擎&#xff0c;提供IUD接口&#xff0c;在 HDFS的数据集上提供了插入更新和增量拉取的流原语。 基础架构图 Hudi特性 ACID事务能…

用户案例 | Apache DolphinScheduler 离线调度在自如多业务场景下的应用与实践

用户案例 | 自如 随着自如业务的快速发展&#xff0c;不断增长的调度任务和历史逾万的存量任务对平台稳定性提出了更高的要求。同时&#xff0c;众多非专业开发人员也需要一种更为“亲民”的调度平台使用体验。 如何满足这些日渐凸显的需求对自如大数据平台的开发团队来说&am…

2023年最全最新的学习课程合集

WEB前端入门&#xff1a;从零开始做网站。 完成所有课堂练习就可以做出自己的作品&#xff0c;并掌握数据库和了解开源项目。 这些课程涵盖了HTML、CSS和JavaScript等前端技术&#xff0c;以及与之相关的设计原则和最佳实践。 前端课程的目标是培养学生在网页开发方面的技能…

冒泡排序模拟实现qsort()函数

冒泡排序模拟实现qsort函数 前言1. 分析2. 解决一&#xff0c;如何接受不同数据3. 解决二&#xff0c;如何实现不同数据的比较4. 解决三&#xff0c;如何实现不同数据交换5. 模拟bubble_sort&#xff08;&#xff09;函数排序整型所有代码实现6. 结构体排序实现7. 结尾 前言 要…

应急管理大屏助力暴雨天气下的水灾防范

随着气候变化和城市化进程的加剧&#xff0c;暴雨天气引发的水灾风险日益凸显。在面对这种自然灾害时&#xff0c;如何高效、及时地应对、减轻损失成为了当务之急。水灾应急管理平台的可视化大屏为相关部门和决策者提供了实时、全面的信息展示和决策支持&#xff0c;大大提升了…