SwiftUIの基本を身につけたい方はこちら

【Xcode/Swift】ローカル通知(プッシュ通知)の実装方法

プッシュ通知とは?

この通知のことです。

実装方法

まずは、このような、通知を許可してもらうアラートを表示させます。

ここで許可してもらわないと通知を送れません。

STEP.1
通知の許可アラート

以下のコードをAppDelegate.swiftdidFinishLaunchingWithOptionsに追記します。この状態ではエラーですが、次の手順で消えますので安心を。

AppDelegate.swift
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, _ in
if granted {
UNUserNotificationCenter.current().delegate = self
}
}
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, _ in if granted { UNUserNotificationCenter.current().delegate = self } }
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, _ in
    if granted {
        UNUserNotificationCenter.current().delegate = self
    }
}

STEP.2
ファグラウンドでも通知を受け取る

以下のコードをAppDelegate.swiftの一番下に追記してください。

AppDelegate.swift
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if #available(iOS 14.0, *) {
completionHandler([[.banner, .list, .sound]])
} else {
completionHandler([[.alert, .sound]])
}
}
}
extension AppDelegate: UNUserNotificationCenterDelegate { func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { if #available(iOS 14.0, *) { completionHandler([[.banner, .list, .sound]]) } else { completionHandler([[.alert, .sound]]) } } }
extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        willPresent notification: UNNotification,
        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            if #available(iOS 14.0, *) {
                completionHandler([[.banner, .list, .sound]])
            } else {
                completionHandler([[.alert, .sound]])
            }
        }
}

これで、通知のアラートの表示ができました。

STEP.3
通知のリクエストを送る

以下の5行のように、書いて通知のリクエストを送ります。今回は、適当にViewController.swiftViewDidLoad()内に書いておきます。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let content = UNMutableNotificationContent()
content.title = "ここに通知のタイトル"
content.body = "ここに通知の本文"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
let request = UNNotificationRequest(identifier: "notification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
let content = UNMutableNotificationContent() content.title = "ここに通知のタイトル" content.body = "ここに通知の本文" let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false) let request = UNNotificationRequest(identifier: "notification", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request)
let content = UNMutableNotificationContent()
content.title = "ここに通知のタイトル"
content.body = "ここに通知の本文"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
let request = UNNotificationRequest(identifier: "notification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)

これでローカル通知の実装が完了しました。

通知の削除

以下のコードで通知を全て削除できます。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()
let center = UNUserNotificationCenter.current() center.removeAllPendingNotificationRequests()
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()

評価