Summary of IOS Push III: Push Classification

Catalog

First: Preface

From the big classification analysis, push in IOS can be divided into local push and remote push.
From a small classification analysis, they can be as follows:

  1. Local push:
    • Timer Interval Notification
    • Calendar Notification
    • Location Notification
  2. Remote push:
    • Ordinary remote push
    • Silent remote push

2: Local push

The classification of local push is based on trigger mode.
General implementation steps for building local push:

1. Building Push Objects: UNMutable Notification Content

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: Take the "attachment" added above as an example. To specify the picture in the project directory in launchImageName, assign the value first in the attachments attachment attributes, otherwise the settings of the launchImageName attributes will not take effect.

2. Building push [trigger] objects: UNNotification Trigger

– UNTimeIntervalNotificationTrigger

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

– UNCalendarNotificationTrigger

//2.create trigger
//Sunday morning 8:00
var components:DateComponents = DateComponents()
components.weekday = 1 //Wekday begins on Sunday.
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. Build push [request] objects: UNNotification Request

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

PS: The identifier value is used to uniquely identify the push message itself. UNNotification Request with the same identifier can modify and override the old push notification.

4. Add [Request] to UNUser Notification Center

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

After adding, the push notification is displayed in the trigger mode set by it.

Three: Long-distance push

Long-distance push can be divided into ordinary long-distance push and silent long-distance push.
Remote Notification has been proposed since IOS7, and together with Background Fetch, it provides a new "background running" mechanism for applications.
Remote Notification is more active and controllable than Background Fetch backstage mechanism. The background service program sends a message body containing "content-available":1 PayLoad content to the APNS server. Then the message sent has the ability of silent push in the background.

  • Compared with ordinary remote push, Remote Notification can call back the specified callback function of silent remote push of APP without opening it, and has a short running time of 30 seconds. As shown in the following figure:

    Its callback function is as follows:
@available(iOS 7.0, *)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
    print("[ Silent push)")
    completionHandler(.noData)
}

Remote Notification has a variety of uses because of its code wake-up function. For example, through remote silent push, push empty updates and other pre-loaded content to the mobile terminal, the pre-loading or uploading function of content can be realized by equipping the system-wide "off-line download and upload" service.

Keywords: iOS Mobile

Added by scrap0346 on Thu, 15 Aug 2019 16:28:43 +0300