iOS开发-NotificationServiceExtension实现实时音视频呼叫通知响铃与震动
iOS开发-NotificationServiceExtension实现实时音视频呼叫通知响铃与震动
在之前的开发中,遇到了实时音视频呼叫通知,当App未打开或者App在后台时候,需要通知到用户,用户点击通知栏后是否接入实时音视频的视频或者音频通话。
在iOS需要为工程新建Target:NotificationServiceExtension
一、主工程配置
需要在主工程中开启推送功能,配置证书。
二、新建NotificationServiceExtension的target
新建NotificationServiceExtension的target
三、实现NotificationService
在NotificationService的方法
// 主要代码
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler- (void)serviceExtensionTimeWillExpire;
在didReceiveNotificationRequest实现呼叫响铃及震动效果。
// 通过通知的Sound设置为voip_call.caf,这里播放一段空白音频,音频结束后结束震动NSString *path = [[NSBundle mainBundle] pathForResource:@"blank_call.mp3" ofType:nil];AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate, NULL, NULL, soundCompleteCallback, NULL);
完整代码如下
#import "NotificationService.h"
#import <AVFoundation/AVFAudio.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>@interface NotificationService ()
{SystemSoundID soundID;
}@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;@property (nonatomic, strong) NSMutableArray *requests;@end@implementation NotificationService- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {self.contentHandler = contentHandler;self.bestAttemptContent = [request.content mutableCopy];BOOL playVoice = NO;NSString *chatPushVo = [self.bestAttemptContent.userInfo objectForKey:@"chatPushVo"];if (chatPushVo && [chatPushVo isKindOfClass:[NSString class]] && chatPushVo > 0) {NSDictionary *dict = [self dictionaryWithJsonString:chatPushVo];if (dict && [dict isKindOfClass:[NSDictionary class]]) {NSString *type = [NSString stringWithFormat:@"%@", [dict objectForKey:@"type"]];// type : 0 呼叫if ([@"0" isEqualToString:type]) {playVoice = YES;// 通过通知的Sound设置为voip_call.caf,这里播放一段空白音频,音频结束后结束震动NSString *path = [[NSBundle mainBundle] pathForResource:@"blank_call.mp3" ofType:nil];AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate, NULL, NULL, soundCompleteCallback, NULL);[self playVoiceWithContent:self.bestAttemptContent.userInfo];}}}if (playVoice == NO) {self.contentHandler(self.bestAttemptContent);}
}- (void)playVoiceWithContent:(NSDictionary *)userInfo {// iOS 10之前前台没有通知栏// 此处调用之前的语音播报的内容[self playRegisterNotifications:userInfo];
}- (void)playRegisterNotifications:(NSDictionary *)userInfo {[self registerNotifications:userInfo];
}- (void)registerNotifications:(NSDictionary *)userInfo {[self startShakeSound];
}/// 开始响铃
- (void)startShakeSound {// 声音AudioServicesPlaySystemSound(soundID);// 震动AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);self.contentHandler(self.bestAttemptContent);
}/// 结束响铃
- (void)stopShakeSound {AudioServicesDisposeSystemSoundID(soundID);AudioServicesRemoveSystemSoundCompletion(soundID);
}//AudioServicesAddSystemSoundCompletion的回调函数
void soundCompleteCallback(SystemSoundID sound,void * clientData) {if (sound == kSystemSoundID_Vibrate) {AudioServicesPlayAlertSound(sound);//重复响铃震动} else {// 移除AudioServicesDisposeSystemSoundID(sound);AudioServicesRemoveSystemSoundCompletion(sound);AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate);AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);}
}- (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);
}/**json转成NSDictionary@param jsonString json字符串@return NSDictionary*/
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {if (jsonString == nil) {return nil;}NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];NSError *err;NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];if(err) {return nil;}return dic;
}@end
四、使用推送
我这里使用的极光推送,注意需要设置推送的mutable-content=1字段。
五、点击通知后处理是否通实时音视频视频通话
在收到通知后,用户点击点击通知后处理是否通实时音视频视频通话逻辑。
在AppDelegate的方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
增加didReceiveRemoteNotification
// 获取启动时收到的APNNSDictionary *userInfo = [[DFPushManager shareInstance] launchOptionsRemoteNotification:launchOptions];if (userInfo) {[self application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult result) {}];}
当收到通知后,在didReceiveRemoteNotification中处理是否接通实时音视频的视频通话的逻辑
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {DFDebugLogger(@"didReceiveRemoteNotification");[[SDPushManager shareInstance] handleRemoteNotification:application userInfo:userInfo completion:nil];NSLog(@"收到通知:%@", userInfo);[[SDPushManager shareInstance] setBadgeNumberMax:nil];if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {// 登录后调用check__weak typeof(self) weakSelf = self;if (userInfo && [userInfo isKindOfClass:[NSDictionary class]]) {NSString *chatPushVo = [userInfo objectForKey:@"chatPushVo"];if (chatPushVo && [chatPushVo isKindOfClass:[NSString class]] && appPushVo.length > 0) {NSDictionary *dict = [DFJsonUtil dictionaryWithJsonString: chatPushVo];if (dict && [dict isKindOfClass:[NSDictionary class]]) {NSString *type = [NSString stringWithFormat:@"%@", [dict objectForKey:@"type"]];// type : 0 呼叫/// 0/用户呼叫// 打开接听页面,进行接听}}}}completionHandler(UIBackgroundFetchResultNewData);
}
六、小结
iOS开发-NotificationServiceExtension实现实时音视频呼叫通知与语音播报
在之前的开发中,遇到了实时音视频呼叫通知,当App未打开或者App在后台时候,需要通知到用户,用户点击通知栏后是否接入实时音视频的视频或者音频通话。
学习记录,每天不停进步。
相关文章:

iOS开发-NotificationServiceExtension实现实时音视频呼叫通知响铃与震动
iOS开发-NotificationServiceExtension实现实时音视频呼叫通知响铃与震动 在之前的开发中,遇到了实时音视频呼叫通知,当App未打开或者App在后台时候,需要通知到用户,用户点击通知栏后是否接入实时音视频的视频或者音频通话。 在…...

性能调试【学习笔记】
什么是调优? 每执行一个Java命令,就分配一个JVM,调优时不要混淆。 根据需求进行JVM规划和预调优优化运行JVM的运行环境(慢、卡顿)解决JVM运行过程中出现的各种问题(内存泄露、内存溢出OOM) 生…...

【taro react】---- 获取元素的位置和宽高等信息
1. 需求分析 添加节点的布局位置的查询请求。相对于显示区域,以像素为单位。其功能类似于 DOM 的 getBoundingClientRect。返回 NodesRef 对应的 SelectorQuery。区分小程序和H5的环境,调用 getBoundingClientRect 获取对应的信息。 2. H5 实现 判断传…...

Java【Spring】项目创建、存储和获取 Bean 的基本方式
文章目录 前言一、创建 Spring 项目1, 创建 Maven 项目2, 添加 Spring 依赖3, 创建启动类 二、存储 Bean 的基本方式1, 创建 Bean2, 存储 Bean 三、获取 Bean 的基本方式1, 获取上下文对象2, 获取 Bean3, 使用 Bean 总结 前言 各位读者好, 我是小陈, 这是我的个人主页, 希望我的…...

docker minio安装
1.介绍 Minio是一款开源的对象存储服务,它可以在任何硬件或云平台上提供高性能、高可用性和高安全性的存储解决方案。Minio最新版是2021年11月发布的RELEASE.2021-11-24T23-19-33Z,它带来了以下几个方面的改进和新特性: - 支持S3 Select AP…...

设计模式-命令模式在Java中的使用示例-桌面程序自定义功能键
场景 欲开发一个桌面版应用程序,该应用程序为用户提供了一系列自定义功能键,用户可以通过这些功能键来实现一些快捷操作。 用户可以将功能键和相应功能绑定在一起,还可以根据需要来修改功能键的设置,而且系统在未来可能还会增加…...

分冶算法 剑指 07 重建二叉树 排序算法:剑指45 把数组排成最小的数 10-I 斐波那契数列
来记录几个注意事项 1.vector容器里利用find()函数 不同于map(map有find方法),vector本身是没有find这一方法,其find是依靠algorithm来实现的。 所以要包含头文件 #include <iostream> #include <…...
Postgresql取消正在执行的任务或强制终止正在执行的任务
Postgresql取消正在执行的任务或强制终止正在执行的任务 要停止 PostgreSQL 数据库中当前正在执行的所有任务,可以使用以下方法: 使用 pg_cancel_backend 函数:连接到 PostgreSQL 数据库,并执行以下命令以停止所有正在执行的任务…...

【Linux】Centos7 的 Systemctl 与 创建系统服务 (shell脚本)
Systemctl systemctl 命令 # 启动 systemctl start NAME.service # 停止 systemctl stop NAME.service # 重启 systemctl restart NAME.service # 查看状态 systemctl status NAME.service # 查看所有激活系统服务 systemctl list-units -t service # 查看所有系统服务 syste…...

Redis集群Cluster搭建
Redis集群Cluster搭建 集群框架1、下载redis2.创建Cluster文件3.修改redis配置文件4.启动redis5.链接各个redis6.分配槽位7.添加从机节点(备份Redis)8.以集群方式登录9.使用开源Redis可视化客户端链接 集群框架 三个集群节点,每个节点有个副本…...
swing组件应用
1. 组件概述 (1) 说明 组件组成Java 的图形界面的各个元素,按照不同的功能,可分为 顶层容器、中间容器、基本组件。顶层容器为java.awt.Window的子类,有JFrame、JDialog等。中间容器和基础组件都为javax.swing.JCompo…...

Spring学习记录----十五、面向切面编程AOP+十六、Spring对事务的支持
十五、面向切面编程AOP IoC使软件组件松耦合。AOP让你能够捕捉系统中经常使用的功能,把它转化成组件。 AOP(Aspect Oriented Programming):面向切面编程,面向方面编程。(AOP是一种编程技术) …...
Color Correction (颜色校正)
介绍 在Unity中,Color Correction (颜色校正) 是一种用于调整场景或游戏画面颜色的技术。其中,Curves(曲线)和Saturation(饱和度)是常用的Color Correction工具。通过Curves,可以对RGB通道进行…...

Unity-缓存池
一、.基础缓存池实现 继承的Singleton脚本为 public class Singleton<T> where T : new() {private static T _instance;public static T GetIstance(){if (_instance null)_instance new T();return _instance;} } 1.PoolManager using System.Collections; using S…...
ubuntu samba 配置常见问题
samba配置: sudo vi /etc/samba/smb.conf [xxx 共享文件名] comment share folder browseable yes writable yes guest ok yes path /workdir/code/favarite create mask 0777 directory mask 0777 sudo /etc/init.d/smbd restart 重启smb服务 以上操作…...

vue3.3-TinyMCE:TinyMCE富文本编辑器基础使用
一、TinyMCE官网 GitHub - tinymce/tinymce TinyMCE中文文档中文手册 二、官网介绍 TinyMCE是一款易用、且功能强大的所见即所得的富文本编辑器。同类程序有:UEditor、Kindeditor、Simditor、CKEditor、wangEditor、Suneditor、froala等等。 TinyMCE的优势&…...

基于以太坊+IPFS的去中心化数据交易方法及平台
自己的论文,哎费事 目录 基于以太坊IPFS的去中心化数据交易方法及平台 基于以太坊IPFS的去中心化数据交易方法及平台 摘要: 数据交易过程中存在数据权属不明和数据安全问题。本文开发了一种基于以太坊IPFS的去中心化数据交易方法及平台。方法包括&am…...
NestJS 的 拦截器 学习
拦截器会用到RxJs,所以在学习拦截器之前可以先了解一下它。 拦截器是使用Injectable()装饰器装饰的类并且实现了接口NestInterceptor。 拦截器受到 AOP(面向切面编程)技术的启发,具有如下的功能: 在方法执行之前/之后绑定额外的逻辑转换函…...

Spring AOP 中的代理对象是怎么创建出来的?
文章目录 1. AOP 用法2. 原理分析2.1 doCreateBean2.2 postProcessAfterInitialization2.3 getAdvicesAndAdvisorsForBean2.3.1 findCandidateAdvisors2.3.2 findAdvisorsThatCanApply2.3.3 extendAdvisors 2.4 createProxy 今天和小伙伴们聊一聊 Spring AOP 中的代理对象是怎么…...

解决@Scope(“prototype“)不生效的问题
目录 Scope(“prototype“)不生效Scope(“prototype“)正确用法——解决Bean多例问题 1.问题,Spring管理的某个Bean需要使用多例2.问题升级3. Spring给出的解决问题的办法(解决Bean链中某个Bean需要多例的问题) Scope(“prototype“)不生效 …...
C++:std::is_convertible
C++标志库中提供is_convertible,可以测试一种类型是否可以转换为另一只类型: template <class From, class To> struct is_convertible; 使用举例: #include <iostream> #include <string>using namespace std;struct A { }; struct B : A { };int main…...

汽车生产虚拟实训中的技能提升与生产优化
在制造业蓬勃发展的大背景下,虚拟教学实训宛如一颗璀璨的新星,正发挥着不可或缺且日益凸显的关键作用,源源不断地为企业的稳健前行与创新发展注入磅礴强大的动力。就以汽车制造企业这一极具代表性的行业主体为例,汽车生产线上各类…...
生成 Git SSH 证书
🔑 1. 生成 SSH 密钥对 在终端(Windows 使用 Git Bash,Mac/Linux 使用 Terminal)执行命令: ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" 参数说明: -t rsa&#x…...

dify打造数据可视化图表
一、概述 在日常工作和学习中,我们经常需要和数据打交道。无论是分析报告、项目展示,还是简单的数据洞察,一个清晰直观的图表,往往能胜过千言万语。 一款能让数据可视化变得超级简单的 MCP Server,由蚂蚁集团 AntV 团队…...
JAVA后端开发——多租户
数据隔离是多租户系统中的核心概念,确保一个租户(在这个系统中可能是一个公司或一个独立的客户)的数据对其他租户是不可见的。在 RuoYi 框架(您当前项目所使用的基础框架)中,这通常是通过在数据表中增加一个…...

回溯算法学习
一、电话号码的字母组合 import java.util.ArrayList; import java.util.List;import javax.management.loading.PrivateClassLoader;public class letterCombinations {private static final String[] KEYPAD {"", //0"", //1"abc", //2"…...

【JVM面试篇】高频八股汇总——类加载和类加载器
目录 1. 讲一下类加载过程? 2. Java创建对象的过程? 3. 对象的生命周期? 4. 类加载器有哪些? 5. 双亲委派模型的作用(好处)? 6. 讲一下类的加载和双亲委派原则? 7. 双亲委派模…...
uniapp 字符包含的相关方法
在uniapp中,如果你想检查一个字符串是否包含另一个子字符串,你可以使用JavaScript中的includes()方法或者indexOf()方法。这两种方法都可以达到目的,但它们在处理方式和返回值上有所不同。 使用includes()方法 includes()方法用于判断一个字…...
Git常用命令完全指南:从入门到精通
Git常用命令完全指南:从入门到精通 一、基础配置命令 1. 用户信息配置 # 设置全局用户名 git config --global user.name "你的名字"# 设置全局邮箱 git config --global user.email "你的邮箱example.com"# 查看所有配置 git config --list…...
上位机开发过程中的设计模式体会(1):工厂方法模式、单例模式和生成器模式
简介 在我的 QT/C 开发工作中,合理运用设计模式极大地提高了代码的可维护性和可扩展性。本文将分享我在实际项目中应用的三种创造型模式:工厂方法模式、单例模式和生成器模式。 1. 工厂模式 (Factory Pattern) 应用场景 在我的 QT 项目中曾经有一个需…...