从大分类分析,IOS中的推送分为:本地推送,远程推送。
从小分类分析,可分别为:
本地推送的分类划分,主要以“触发方式”为标准进行划分的。
构建本地推送的一般实现步骤:
self.content = UNMutableNotificationContent()
self.content?.title = "Title"
self.content?.subtitle = "subTitle"
self.content?.body = "This is a body"
self.content?.badge = 1
if let path:String = Bundle.main.path(forResource: "im_phone@3x", ofType: "png"){
do{
let attachment:UNNotificationAttachment = try UNNotificationAttachment(identifier: "im_phone", url: URL(fileURLWithPath: path), options: nil)
self.content?.attachments = [attachment]
}catch{}
}
self.content?.launchImageName = "im_phone@3x"
self.content?.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "new_order.mp3"))
PS: 以上面添加的“附件”为例,在launchImageName中指定项目目录中的图片,要先在attachments附件属性进行先设置赋值,否则launchImageName属性的设置不生效。
本地推送通知有三种类型的【触发】对象,应用程序通过运行代码的方式,主动向“推送中心”申请这三种类型的“本地推送”,系统自动向应用发起本地推送,无论应用是否启动。
这三种类型的【触发】对象的介绍及简单代码如下:
//2.create trigger
let trigger:UNTimeIntervalNotificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
//2.create trigger
//周日早上 8:00
var components:DateComponents = DateComponents()
components.weekday = 1 //weekday是从周日开始的
components.hour = 8
let trigger:UNCalendarNotificationTrigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
//2.create trigger
let region:CLCircularRegion = CLCircularRegion()
let trigger:UNLocationNotificationTrigger = UNLocationNotificationTrigger(region: region, repeats: true)
//3.create Request
let identifier:String = "trigger_identifier"
let request:UNNotificationRequest = UNNotificationRequest(identifier: identifier, content: self.content!, trigger: trigger)
PS: identifier值用于唯一标识推送信息本身,具有相同identifier的UNNotificationRequest,可以实现修改和覆盖旧的推送通知。
//4.add Notification
UNUserNotificationCenter.current().add(request) { (error:Error?) in
print("*** [Error] \(error?.localizedDescription ?? "") ***")
}
添加后,推送通知以其设置的触发方式进行展示。
运程推送都是走APNS流程,实现应用后台向指定的手机端推送消息。远程推送从展示方式划分可分为:普通远程推送,静默远程推送。
“普通远程推送”只是相对于“静默远程推送”而言的,从代码层面分析,其回调的方法与“静默远程推送”不一样。分为应用处于前台的消息回调函数
,及推送消息被点击的回调函数
。
一般的“普通远程推送”,其目的在于能过通知等方式,让用户感知到消息的推送通知
,如消息弹框,声单和震动等。
“静默远程推送”的提出,更多是为应用提供多一种后台主动唤醒机制
,与Background Fetch
被动唤醒机制相比,运用“静默远程推送”可以由应用服务主动唤醒应用。
在很多的静默远程推送应用中,主要解决如何主动唤醒应用,运行一段代码而不被用户察觉
。比如预先下载文档,清除或重新计算显示应用Badge的数值等功能实现都可以通过静默远程推送实现。所以运用静默远程推送,可以实现主动激活应用,进行逻辑处理,或进行离线下载等。
静默远程推送(Remote Notification)从IOS7开始提出,其与 Background Fetch一起,为应用提供新的“后台运行”机制。
与Background Fetch后台机制的不定时唤醒相比,Remote Notification更具有主动可控性。后台服务程序,通过向APNS服务器发送包含"content-available" : 1的PayLoad内容的消息体。那么发送的消息就具有后台静默推送能力了。
@available(iOS 7.0, *)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
print("【 静默推送 】")
completionHandler(.noData)
}
Remote Notification因为其具有代码唤醒功能,其用途就变得多种多样。如通过远程静默推送,向手机端推送内空更新及其它预加载内容,可通过配全系统的“离线下载及上传”服务,实现内容的预加载或上传功能。
静默远程推送在不同的payload内容的测试中发现有如下特点:
"aps" : { "content-available" : 1 }
当没有设置其它属性时,应用成功回调didReceiveRemoteNotification
方法,没有声音没提示等。
"aps" : { "badge" : 21, "content-available" : 1 }
badge属性设置生效。当应用处理于前台时,“前台回调”方法willPresent
先收到回调,之后didReceiveRemoteNotification
方法执行回调。
"aps" : { "sound" : "default", "content-available" : 1 }
sound属性设置生效。当应用处理于前台时,“前台回调”方法willPresent
先收到回调,之后didReceiveRemoteNotification
方法执行回调。
"aps" : { "alert" : "大家快来看看", "content-available" : 1 }
alert属性设置生效。当应用处理于前台时,“前台回调”方法willPresent
先收到回调,之后didReceiveRemoteNotification
方法执行回调。设置title,body同理。
结论:当"content-available" : 1
状态下,同时设置了其它普通推送属性,且属性值不为空。应用端在接收到消息后,会先走普通推送
的回调方法,再走静默远程推送的didReceiveRemoteNotification
方法