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

iOS 通知

iOS 通知分为本地推送和远程推送两类

一. 本地推送使用流程

1. 注册通知
//请求通知权限
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {if (error) { NSLog(@"request authorization error: %@", error); }
}];//注册通知
[[UIApplication sharedApplication] registerForRemoteNotifications];
2.推送通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; //标题
content.sound = [UNNotificationSound defaultSound];NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
content.badge = @(badge);
content.body = message;// 本地推送一定要有内容,即body不能为空。#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000if (@available(iOS 15.0, *)) {content.interruptionLevel = UNNotificationInterruptionLevelTimeSensitive;//会使手机亮屏且会播放声音;可能会在免打扰模式(焦点模式)下展示// @"{"aps":{"interruption-level":"time-sensitive"}}";// @"{"aps":{"interruption-level":"active"}}";content.body = message;// 本地推送一定要有内容,即body不能为空。}
#endif// repeats,是否重复,如果重复的话时间必须大于60s,要不会报错
//UNCalendarNotificationTrigger 如果是时间触发
//UNLocationNotificationTrigger 地点触发UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.01  repeats:NO];/* */
//添加通知的标识符,可以用于移除,更新等搡作
NSString * identifier = [[NSUUID UUID] UUIDString];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) 
{if(!error){dispatch_async(dispatch_get_main_queue(), ^{});}
}];//点击本地通知回调 
/*API_DEPRECATED("Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]"*/- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{//
}

二.远程推送(APNS)

1. 远程推送的五步骤

1).应该程序注册APNS消息推送

2).ios 从APNS Server得到devicetoken.

3).应用程将devicetoken发送从自己的服务器。

4).服务器将消息发送给APNS服务器。

5).APNS发送给app

苹果官司方给机制解释图

2. 远程推送大小限制

远程通知负载的大小根据服务器使用的API不同而不同

当使用HTTP/2 provider API时,负载最大为4kB;当使用legacy binary interface时,负载最大为2kB。当负载大小超过规定的负载大小时,APNs会拒绝发送此通知 

3.推送通知实现
1)首先 设置 app 的bundle Id 支持 通知推送

2)申请推送通知证书,这个证书主要是服务器需要使用的

推送证书的申请过程和其它证书类似

3)app 注册通知 这部分和本地通知是一样的。
//请求通知权限
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {if (error) { NSLog(@"request authorization error: %@", error); }
}];//注册通知
[[UIApplication sharedApplication] registerForRemoteNotifications];
4)收到APNS 发送的token 并发送给服务器
- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{//注册推送消息成功//收到deviceToken并发送给服务器NSLog(@"deviceToken = %@", deviceToken);
}- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error API_AVAILABLE(ios(3.0))
{//注册推送消息失败
}

服务器收到token后,需要发送消息时,就可以把token,消息内容和证书一起发送给

APNS服务器,APNS就推送给app了。

5)app收到推送通知后的处理

1> app被杀死时,用户点击通知会在didFinishLaunchingWithOptions获取通知内容

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {...// 注册通知类型
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {if (error) { NSLog(@"request authorization error: %@", error); }
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];// app 被杀死时用户点击通知 获取推送内容
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {//能过点击通知启动appNSLog(@"userInfo = %@", userInfo);}...}

2> app在后台,点击通知

//老版处理
/*- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{DDLogDebug(@"userInfo ----->%@",userInfo);
}*///新版处理
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler
{DDLogDebug(@"userInfo ----->%@",userInfo);
}

三.推送功能的测试

有个推送测试的工具 SmartPush

下载地址:

1、Github地址:https://github.com/shaojiankui/SmartPush

 2、release安装包:https://github.com/shaojiankui/SmartPush/releases

 

3、消息体格式: 

{"aps": {"alert": {"title": "新消息","body": "您有一条新的消息!"},"badge": 1,"sound": "default"},"customData": {"type": "message","id": "12345"}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 笔记:Echarts柱状图 实现滚轮条 数据太多
  • 使用 java 将 excel 文件转 html、pdf ,以及 pdf 合并和拆分
  • 使用Node-RED实现和部署物联网入侵检测的机器学习管道
  • C++基础语法:while的使用
  • 数据结构--树(笔记)
  • 2025计算机毕设:50条小众好做的SSM题目推荐【计算机毕设选题推荐】
  • 服务器配置miniconda环境
  • npm install 安装报错解决指南
  • 什么是BOM,有哪些分类?
  • Notion使用详解
  • WPF 动画 插值动画、关键帧动画、路径动画
  • Pod基础使用
  • websocket拦截插件
  • 无线数传模块有啥特点?
  • 万象公文常见问题的处理方法
  • 【跃迁之路】【733天】程序员高效学习方法论探索系列(实验阶段490-2019.2.23)...
  • ComponentOne 2017 V2版本正式发布
  • JAVA_NIO系列——Channel和Buffer详解
  • Javascript编码规范
  • Laravel深入学习6 - 应用体系结构:解耦事件处理器
  • PHP面试之三:MySQL数据库
  • Python学习之路13-记分
  • Redis学习笔记 - pipline(流水线、管道)
  • Spring Cloud(3) - 服务治理: Spring Cloud Eureka
  • SpringCloud集成分布式事务LCN (一)
  • vue:响应原理
  • WePY 在小程序性能调优上做出的探究
  • 爱情 北京女病人
  • 后端_ThinkPHP5
  • 缓存与缓冲
  • 马上搞懂 GeoJSON
  • 目录与文件属性:编写ls
  • 算法-图和图算法
  • 协程
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • 我们雇佣了一只大猴子...
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • #DBA杂记1
  • #if等命令的学习
  • %3cscript放入php,跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击
  • (02)vite环境变量配置
  • (11)工业界推荐系统-小红书推荐场景及内部实践【粗排三塔模型】
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (二)springcloud实战之config配置中心
  • (二)斐波那契Fabonacci函数
  • (二开)Flink 修改源码拓展 SQL 语法
  • (附源码)spring boot北京冬奥会志愿者报名系统 毕业设计 150947
  • (四)React组件、useState、组件样式
  • (译)计算距离、方位和更多经纬度之间的点
  • (转)VC++中ondraw在什么时候调用的
  • (转)Windows2003安全设置/维护
  • (转)如何上传第三方jar包至Maven私服让maven项目可以使用第三方jar包
  • .“空心村”成因分析及解决对策122344