当前位置: 首页 > news >正文

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实现实时音视频呼叫通知响铃与震动 在之前的开发中&#xff0c;遇到了实时音视频呼叫通知&#xff0c;当App未打开或者App在后台时候&#xff0c;需要通知到用户&#xff0c;用户点击通知栏后是否接入实时音视频的视频或者音频通话。 在…...

性能调试【学习笔记】

什么是调优&#xff1f; 每执行一个Java命令&#xff0c;就分配一个JVM&#xff0c;调优时不要混淆。 根据需求进行JVM规划和预调优优化运行JVM的运行环境&#xff08;慢、卡顿&#xff09;解决JVM运行过程中出现的各种问题&#xff08;内存泄露、内存溢出OOM&#xff09; 生…...

【taro react】---- 获取元素的位置和宽高等信息

1. 需求分析 添加节点的布局位置的查询请求。相对于显示区域&#xff0c;以像素为单位。其功能类似于 DOM 的 getBoundingClientRect。返回 NodesRef 对应的 SelectorQuery。区分小程序和H5的环境&#xff0c;调用 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是一款开源的对象存储服务&#xff0c;它可以在任何硬件或云平台上提供高性能、高可用性和高安全性的存储解决方案。Minio最新版是2021年11月发布的RELEASE.2021-11-24T23-19-33Z&#xff0c;它带来了以下几个方面的改进和新特性&#xff1a; - 支持S3 Select AP…...

设计模式-命令模式在Java中的使用示例-桌面程序自定义功能键

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

分冶算法 剑指 07 重建二叉树 排序算法:剑指45 把数组排成最小的数 10-I 斐波那契数列

来记录几个注意事项 1.vector容器里利用find&#xff08;&#xff09;函数 不同于map&#xff08;map有find方法&#xff09;&#xff0c;vector本身是没有find这一方法&#xff0c;其find是依靠algorithm来实现的。 所以要包含头文件 #include <iostream> #include <…...

Postgresql取消正在执行的任务或强制终止正在执行的任务

Postgresql取消正在执行的任务或强制终止正在执行的任务 要停止 PostgreSQL 数据库中当前正在执行的所有任务&#xff0c;可以使用以下方法&#xff1a; 使用 pg_cancel_backend 函数&#xff1a;连接到 PostgreSQL 数据库&#xff0c;并执行以下命令以停止所有正在执行的任务…...

【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.添加从机节点&#xff08;备份Redis&#xff09;8.以集群方式登录9.使用开源Redis可视化客户端链接 集群框架 三个集群节点&#xff0c;每个节点有个副本…...

swing组件应用

1. 组件概述 &#xff08;1&#xff09; 说明 组件组成Java 的图形界面的各个元素&#xff0c;按照不同的功能&#xff0c;可分为 顶层容器、中间容器、基本组件。顶层容器为java.awt.Window的子类&#xff0c;有JFrame、JDialog等。中间容器和基础组件都为javax.swing.JCompo…...

Spring学习记录----十五、面向切面编程AOP+十六、Spring对事务的支持

十五、面向切面编程AOP IoC使软件组件松耦合。AOP让你能够捕捉系统中经常使用的功能&#xff0c;把它转化成组件。 AOP&#xff08;Aspect Oriented Programming&#xff09;&#xff1a;面向切面编程&#xff0c;面向方面编程。&#xff08;AOP是一种编程技术&#xff09; …...

Color Correction (颜色校正)

介绍 在Unity中&#xff0c;Color Correction (颜色校正) 是一种用于调整场景或游戏画面颜色的技术。其中&#xff0c;Curves&#xff08;曲线&#xff09;和Saturation&#xff08;饱和度&#xff09;是常用的Color Correction工具。通过Curves&#xff0c;可以对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配置&#xff1a; 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是一款易用、且功能强大的所见即所得的富文本编辑器。同类程序有&#xff1a;UEditor、Kindeditor、Simditor、CKEditor、wangEditor、Suneditor、froala等等。 TinyMCE的优势&…...

基于以太坊+IPFS的去中心化数据交易方法及平台

自己的论文&#xff0c;哎费事 目录 基于以太坊IPFS的去中心化数据交易方法及平台 基于以太坊IPFS的去中心化数据交易方法及平台 摘要&#xff1a; 数据交易过程中存在数据权属不明和数据安全问题。本文开发了一种基于以太坊IPFS的去中心化数据交易方法及平台。方法包括&am…...

NestJS 的 拦截器 学习

拦截器会用到RxJs&#xff0c;所以在学习拦截器之前可以先了解一下它。 拦截器是使用Injectable()装饰器装饰的类并且实现了接口NestInterceptor。 拦截器受到 AOP(面向切面编程)技术的启发&#xff0c;具有如下的功能&#xff1a; 在方法执行之前/之后绑定额外的逻辑转换函…...

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.问题&#xff0c;Spring管理的某个Bean需要使用多例2.问题升级3. Spring给出的解决问题的办法&#xff08;解决Bean链中某个Bean需要多例的问题&#xff09; Scope(“prototype“)不生效 …...

如何3步打造电影级Minecraft画面:Revelation光影包完整配置指南

如何3步打造电影级Minecraft画面&#xff1a;Revelation光影包完整配置指南 【免费下载链接】Revelation An explorative shaderpack for Minecraft: Java Edition 项目地址: https://gitcode.com/gh_mirrors/re/Revelation 你是否厌倦了Minecraft中单调的光影效果&…...

JavaScript中对象属性存在的四种检测方法性能评估

检测自有属性用hasOwnProperty()最常用高效&#xff0c;检测自有继承属性用in操作符最自然&#xff1b;避免Object.keys().includes()因性能差且语义冗余&#xff1b;安全场景用Object.prototype.hasOwnProperty.call()。在 JavaScript 中检测对象属性是否存在&#xff0c;常用…...

分类数据集 - CT图像脊柱骨折检测图像分类数据集下载

数据集介绍&#xff1a;CT图像脊柱骨折检测图像分类数据集&#xff0c;真实临床采集高质量脊柱CT断层扫描图片数据&#xff1b;适用实际项目应用&#xff1a;CT图像脊柱骨折检测图像分类项目&#xff0c;脊柱创伤辅助诊断系统&#xff0c;以及作为通用脊柱骨折检测数据集场景数…...

Hermes Agent Loop:从9200行代码中读懂Agent心脏

翻开 Hermes Agent 的源码&#xff0c;run_agent.py。 9200 行。 看完之后&#xff0c;我意识到一件事。 Agent 的心脏&#xff0c;不是模型。 不是工具。 是 Agent Loop。 模型调用 → 工具执行 → 循环 → 直到完成。 这个循环&#xff0c;决定了 Agent 能做什么&…...

Qwen3.5-9B-GGUF部署案例:出版机构AI编辑助手与稿件初筛系统实践

Qwen3.5-9B-GGUF部署案例&#xff1a;出版机构AI编辑助手与稿件初筛系统实践 1. 项目背景与价值 在传统出版行业&#xff0c;编辑团队每天需要处理大量投稿稿件&#xff0c;面临以下痛点&#xff1a; 稿件质量参差不齐&#xff0c;人工初筛耗时耗力编辑校对工作重复性高&…...

BetterNCM Installer:一键解锁网易云音乐无限插件体验的终极神器

BetterNCM Installer&#xff1a;一键解锁网易云音乐无限插件体验的终极神器 【免费下载链接】BetterNCM-Installer 一键安装 Better 系软件 项目地址: https://gitcode.com/gh_mirrors/be/BetterNCM-Installer 想要让网易云音乐变得更强大、更个性化吗&#xff1f;Bett…...

Phi-mini-MoE-instruct多语言效果:中→英→法→中回译保真度测试与语义一致性分析

Phi-mini-MoE-instruct多语言效果&#xff1a;中→英→法→中回译保真度测试与语义一致性分析 1. 项目背景与模型特点 Phi-mini-MoE-instruct是一款轻量级混合专家&#xff08;MoE&#xff09;指令型小语言模型&#xff0c;在多项基准测试中展现出卓越性能。该模型采用创新的…...

从新手到高手:我踩过的PyTorch布尔转浮点那些坑,以及一个被低估的`.to()`方法

从新手到高手&#xff1a;PyTorch布尔转浮点的深度探索与.to()方法实战指南 第一次接触PyTorch时&#xff0c;我被它的灵活性和强大功能所吸引&#xff0c;但同时也被一些看似简单的问题困扰——比如如何优雅地将布尔张量转换为浮点张量。这个问题看似微不足道&#xff0c;却折…...

VR党建蛋椅|以沉浸式体验推动党建学习方式创新

在信息化、数字化不断发展的背景下&#xff0c;党建学习方式也在持续升级。传统的集中学习、展板阅读虽然依然发挥着重要作用&#xff0c;但在互动性、沉浸感和吸引力方面存在一定局限。VR党建蛋椅正是在这一背景下诞生的一种创新型党建学习设备&#xff0c;通过虚拟现实技术与…...

COMSOL多孔介质流燃烧器模型:四场耦合,多物理场涉及非等温反应流场模拟

comsol多孔介质流燃烧器模型&#xff0c;集层流流动模块&#xff0c;流体传热模块&#xff0c;浓物质传递模块和化学反应模块于一体&#xff0c;四场耦合&#xff0c;多物理场涉及非等温流动场&#xff0c;反应流场。经实测可以精确的模拟燃烧流动耦合的仿真结果&#xff0c;适…...