IOS推送相關總結三:推送分類

一:前言

從大分類分析,IOS中的推送分爲:本地推送,遠程推送。
從小分類分析,可分別爲:

  1. 本地推送:
    • Timer Interval Notification
    • Calendar Notification
    • Location Notification
  2. 遠程推送:
    • 普通遠程推送
    • 靜默遠程推送

二:本地推送

本地推送的分類劃分,主要以“觸發方式”爲標準進行劃分的。
構建本地推送的一般實現步驟:

1. 構建推送【內容】對象:UNMutableNotificationContent

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. 構建推送【觸發】對象:UNNotificationTrigger

– UNTimeIntervalNotificationTrigger

//2.create trigger
let trigger:UNTimeIntervalNotificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)

– UNCalendarNotificationTrigger

//2.create trigger
//週日早上 8:00
var components:DateComponents = DateComponents()
components.weekday = 1 //weekday是從週日開始的
components.hour = 8
let trigger:UNCalendarNotificationTrigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)

– UNLocationNotificationTrigger

//2.create trigger
let region:CLCircularRegion = CLCircularRegion()
let trigger:UNLocationNotificationTrigger = UNLocationNotificationTrigger(region: region, repeats: true)

3. 構建推送【請求】對象:UNNotificationRequest

//3.create Request
let identifier:String = "trigger_identifier"
let request:UNNotificationRequest = UNNotificationRequest(identifier: identifier, content: self.content!, trigger: trigger)

PS: identifier值用於唯一標識推送信息本身,具有相同identifier的UNNotificationRequest,可以實現修改和覆蓋舊的推送通知。

4. 向UNUserNotificationCenter中添加【請求】

//4.add Notification
UNUserNotificationCenter.current().add(request) { (error:Error?) in
   print("*** [Error] \(error?.localizedDescription ?? "") ***")
}

添加後,推送通知以其設置的觸發方式進行展示。

三:遠程推送

遠程推送從展示方式劃分可分爲:普通遠程推送,靜默遠程推送。
靜默遠程推送(Remote Notification)從IOS7開始提出,其與 Background Fetch一起,爲應用提供新的“後臺運行”機制。
與Background Fetch後臺機制的不定時喚醒相比,Remote Notification更具有主動可控性。後臺服務程序,通過向APNS服務器發送包含"content-available" : 1的PayLoad內容的消息體。那麼發送的消息就具有後臺靜默推送能力了。

  • 與普通遠程推送相比,Remote Notification可以在APP不打開的情況下,對APP的中靜默遠程推送指定的回調函數進行回調,並具有短暫的30秒運行時間。如下圖所示:
    在這裏插入圖片描述
    其回調函數如下:

@available(iOS 7.0, *)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
    print("【 靜默推送 】")
    completionHandler(.noData)
}

Remote Notification因爲其具有代碼喚醒功能,其用途就變得多種多樣。如通過遠程靜默推送,向手機端推送內空更新及其它預加載內容,可通過配全系統的“離線下載及上傳”服務,實現內容的預加載或上傳功能。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章