How do local message notification using UserNotifications

final effect

Notification
Notification

Request permission notice

import UserNotifications

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (allowed, error) in
    if !allowed {
        // User prohibited notification authority
    }
}

The above code is the code request notification authority, put it where you want to request permission, here I put it AppDelegate.swiftin, in fact, the actual project is relatively unreasonable request on here, where do DEMO it does not matter.

Permission Request

Creating a notification message

The content of the notification message

Create here a simple message notification, when you open App when, after five seconds to trigger a notification.

import UIKit
import UserNotifications

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // notification content
        let content = UNMutableNotificationContent()
        content.title = "大板栗"
        content.subtitle = "这是副标题"
        content.badge = 1
        content.body = "这是通知消息的主要内容"
        content.sound = UNNotificationSound(named: "sound")
    }
}

The content of the notification message UNMutableNotificationContentmainly includes these attributes:

  • .title: Title
  • .subtitle: Subtitle
  • .badge: Subscript
  • .body: Message body content
  • .sound: Sound notification message

Identifier notification message

Each request message has a request identifier, which is a string, and must be unique, it can be used to replace or remove a pendingrequest state.

let identify = "io.justx.openApp"

Trigger notification message

Notification message also needs a "trigger" to inform the notification center "when" should trigger message, if you do not specify the "trigger", then this represents the request message immediately.

UserNotifications It offers three "trigger":

  • UNTimeIntervalNotificationTrigger: After a period of time trigger
  • UNCalendarNotificationTrigger: Specifies the Time-Triggered
  • UNLocationNotificationTrigger: Specify the location of the trigger range

After a little time trigger :

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

timeIntervalRepresents the "seconds", not "ms" repeatindicates whether repeated.

Specified time trigger :

var dateComponents = DateComponents()
components.year = 2018
compon 大专栏  如何使用 UserNotifications 做本地消息通知ents.month = 09
components.day = 05
components.hour = 20
components.minute = 13
components.second = 14

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

The above code indicates that the notification will be in 2018年9月5日20点13分14秒when they were triggered. The code below said notification will be in 每周六的上午8点30分when they were triggered.

var dateComponents = DateComponents()
components.weekday = 7 // 周六
components.hour = 8 // 早上 8 点
components.second = 30 // 30 分

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

Specify the location of the trigger range :

In addition to the defined time notification message is triggered from the "time dimension", also from "spatial dimension" to indicate when it should be triggered.

First you need to define a "coordinate" and "regional", followed by the definition of when "enter" or "left" in this area of ​​the trigger message notification, the specific code as follows:

// 定义一个坐标
let coordinate = CLLocationCoordinate2D(latitude: 52.10, longitude: 51.11)
// 定义一个以该坐标为圆心,半径为 1000 米的「区域」
let region = CLCircularRegion(center: coordinate, radius: 1000, identifier: "center")
region.notifyOnEntry = true  // 进入此范围时触发消息通知
region.notifyOnExit = false  // 离开此范围时不触发消息通知
let trigger = UNLocationNotificationTrigger(region: region, repeats: true)

Request notification message

Notification message "Content", after a good "identifier" and the definition of "committing an" it can generate the corresponding "notification request" a:

let request = UNNotificationRequest(identifier: identify, content: content, trigger: trigger)

The notification message is added to the request message center

Each message notification in fact a "request" When we defined the "request", also need to add it to the "message center" to be able to take effect:

// add request to notification center
UNUserNotificationCenter.current().add(request) { error in
    if error == nil {
        //
    }
}

The complete code

import UIKit
import UserNotifications

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // notification content
        let content = UNMutableNotificationContent()
        content.title = "大板栗"
        content.subtitle = "这是副标题"
        content.badge = 1
        content.body = "这是通知消息的主要内容"
        content.sound = UNNotificationSound(named: "sound")

        // The unique identifier for this notification request.
        // It can be used to replace or remove a pending notification request or a delivered notification.
        let identify = "io.justx.openApp"
        // The trigger that will or did cause the notification to be delivered. No trigger means deliver now.
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
        let request = UNNotificationRequest(identifier: identify, content: content, trigger: trigger)

        // add request to notification center
        UNUserNotificationCenter.current().add(request) { error in
            if error == nil {
                //
            }
        }
    }
}

Guess you like

Origin www.cnblogs.com/sanxiandoupi/p/11693254.html