どのようにUserNotificationsを使用してローカルメッセージ通知を行います

最終結果

通知
通知

要求許可通知

import UserNotifications

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

上記のコードは、あなたが許可を要求する場所コード要求通知機関は、それを入れている、私はそれを置くここAppDelegate.swiftでは、実際には、実際のプロジェクトでは、DEMOを行い、ここでは比較的無理の要求であり、それは関係ありません。

許可要求

通知メッセージを作成します

通知メッセージの内容

5秒後に通知をトリガするとき、あなたがアプリを開くと、ここでは簡単なメッセージ通知を作成します。

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")
    }
}

通知メッセージの内容は、UNMutableNotificationContent主にこれらの属性が含まれています。

  • .title:タイトル
  • .subtitle:字幕
  • .badge:添字
  • .body:メッセージ本文の内容
  • .sound:サウンド通知メッセージ

識別子通知メッセージ

各要求メッセージは、文字列である要求識別子を持ち、かつ一意である必要があり、交換または除去するために使用することができるpending要求状態を。

let identify = "io.justx.openApp"

トリガー通知メッセージ

通知メッセージはまた、「トリガー」を指定しない場合には、これはすぐに要求メッセージを表し、メッセージをトリガする必要があります「と、」通知センターに通知する「トリガー」を必要とします。

UserNotifications これは3つの「トリガー」を提供しています:

  • UNTimeIntervalNotificationTrigger:タイムトリガの期間の後
  • UNCalendarNotificationTrigger:タイムトリガを指定します。
  • UNLocationNotificationTrigger:トリガ範囲の場所を指定

少し時間トリガの後に

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

timeInterval「秒」を表し、ない「MS」はrepeat繰り返しかどうかを示します。

指定されたタイムトリガ

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)

上記のコードは、通知が中になることを示し2018年9月5日20点13分14秒、それらがトリガされたとき。以下のコードは、通知が中になると述べ每周六的上午8点30分、それらがトリガされたとき。

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

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

トリガー範囲の場所を指定します

定義された時間に加えて、通知メッセージは、それがトリガされるべきときを示すために、また、「空間次元」から、「時間ディメンション」からトリガされます。

まず、次のように、「入力」またはトリガメッセージ通知のこの分野での「左」ときの定義に続いて、「座標」と「地域」とは、特定のコードを定義する必要があります。

// 定义一个坐标
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)

要求通知メッセージ

通知の良い「識別子」の後にメッセージ「コンテンツ」、そしてそれは、対応する「通知要求」を生成することができます「コミット」の定義:

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

通知メッセージは、要求メッセージセンターに追加され

私たちは、「要求」を定義した場合、実際には各メッセージの通知「要求」は、また、効果を取ることができるように、「メッセージセンター」に追加する必要があります。

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

完全なコード

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 {
                //
            }
        }
    }
}

おすすめ

転載: www.cnblogs.com/sanxiandoupi/p/11693254.html