iOS推送通知概览

iOS推送通知概览

一、响应推送(本地通知 和 远程通知 都合适)

iOS 10 以前

1. UIUserNotificationAction

用于定义一个动作
1)identifier:标识,Application delegate 回调时辨识
2)title:通知界面的button title
3)activationMode:决定该动作对在前台还是后台,是否需要解锁iPhone
4)behavior:可以在通知界面中和用户交互,参考微信的通知界面回复好友消息

2. UIUserNotificationCategory

用于 group 多个action
1)identifier:标识,定义通知时,可以指定响应的category
2)可以为动作设置context,如 bannner就是一个context, 只能显示两个button;

3. UIUserNotificationSettings

用于 group 多个 category

1)types:设置 badge、banner、alert(声音)
最终向 application 单例对象注册 setttngs,请求用户授权,具体回调查看苹果文档

4.推送到达的时候,具体回调看 UIApplicationDelegate 文档

iOS 10 及以后

  1. UNNotificationAction : 和 UIUserNotificationAction 用法相似

  2. UNNotificationCategory: 和UIUserNotificationCategory 用法相似

  3. UNUserNotificationCenter

用于向用户请求授权,并可以设置 badge、banner、alert(声音),充当了之前 application 和 setttings 的角色

  1. 推送到达的时候, 具体回调看 UNUserNotificationCenter 文档

二、实现远程通知概览

更详细看苹果文档:https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/HandlingRemoteNotifications.html
或者在 搜索:Configuring Remote Notification Support

实现概览:
a.开启远程通知
b.app注册远程通知服务,并获取 device token
c.发送设备 token 到后台服务器(标识设备)
d.app实现对远程通知的处理

注⚠️:
a.APN向远程应用程序发送远程通知的能力要求应用程序至少启动一次。
b.在iOS设备上,如果用户使用应用程序多任务UI强制退出应用程序,则在用户重新启动应用程序之前,应用程序不会收到远程通知

三、APNs和Provider

若要真正实现,详读以下链接内容
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1

1.连接:

a. APNs <— — -> app(苹果自动实现)
应用首次启动时,iOS会在 APNs 和 app 建立稳定的IP连接,app 利用这个连接可以做获取推送的初始化动作(注册推送)
b. provider - - - > APNs
(乐逗)服务器 和 APNs 的连接,用于推送消息到APNs。要求在开发者帐号上做相关配置,以及使用 Apple 提供的加密证书进行连接。

2.存储-推送

APNsf加入了QoS,实现了 存储-推送 的功能。如果iOS设备离线,APNs在一段时间内存储这个通知,并在设备在线时再次发送,但是APNs只会存储最近的一条通知。如果设备长时间离线,APNs会放弃所有的通知存储。

3.provider(乐逗服务器)要做的事情

a.从 运行在用户设备上的 app 获取 DeviceToken(app主动发送给后台), DeviceToken 用于标识一个 app 实例
b.决定推送时机
c.构建和发送request,request包含要推送的信息。

推送request有以下要求:
¡. 构建有效JSON词典,包含要推送的信息。
⚠️详情见:https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH10-SW1

¡¡. request的发送需要使用 HTTP2.0 协议
关于 HTTP2.0请求格式和可能 response 和 error, 见 https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1

¡¡¡.在安全和持续的网络通道,向APNs发送的请求要包括加密的身份(token(JWT)方式或者certificate方式)。
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW9

你可能感兴趣的:(iOS推送通知概览)