LoginSignup
3
2

UIKitローカルプッシュ通知最小実装

Posted at

はじめに

プッシュ通知について何も知らなかったのでメモです

ローカル通知とリモート通知の違い

ローカル通知:この通知はユーザーのデバイス上のアプリケーション自体から直接送信されます。例えば、特定の時間になったらリマインダーを表示するなど

リモート通知(またはプッシュ通知):これらの通知は、サーバーなどのリモートシステムからデバイスへ送信されます。例えば、新しいメールが来たとき、ソーシャルメディアで新しいいいねがあったとき、またはアプリの新機能やキャンペーンなどの情報をユーザーに知らせるときなど

ローカル通知はサーバ用意しなくても通知ができるってことです

UserNotifications Framework

UserNotifications Framework:このフレームワークを使用すると、ローカル通知やリモート通知をスケジュールし、表示することができます

できるもの

通知を許可するアラートがでて

Simulator Screen Shot - iPhone 14 Pro - 2023-05-17 at 20.15.37.png

Buttonをタップすると

Simulator Screen Shot - iPhone 14 Pro - 2023-04-21 at 11.58.16.png

5秒後にこのアプリ外で通知がでます

Simulator Screen Shot - iPhone 14 Pro - 2023-05-17 at 20.15.37.png

最小実装

Main.storyboard

スクリーンショット 2023-05-17 20.22.10.png

ViewContoller

import UIKit
import UserNotifications

class ViewController: UIViewController {

    @IBOutlet private weak var button: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()

        // 通知許可を求める
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            if granted {
                print("通知許可が得られました")
            }
        }
        button.setTitle("ローカル通知", for: .normal)
        button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        


    }

    @objc func buttonTapped() {
        // 通知内容を設定
        let content = UNMutableNotificationContent()
        content.title = "通知でーす"
        content.body = "こんちわ"
        content.sound = UNNotificationSound.default

        // 5秒後に通知する
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

        // 通知リクエストを作成
        let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)

        // 通知センターに通知リクエストを登録
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.add(request) { (error) in
            if let error = error {
                print("通知リクエストの追加に失敗しました: \(error.localizedDescription)")
            }
        }
    }
}

このサンプルだと大きく分けて4つの部品があります

1、通知許可を求める

let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            if granted {
                print("通知許可が得られました")
            }

2、通知内容を設定

let content = UNMutableNotificationContent()
        content.title = "通知でーす"
        content.body = "こんちわ"
        content.sound = UNNotificationSound.default

3、いつ通知するか決める

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

4、通知を登録

let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.add(request) { (error) in
            if let error = error {
                print("通知リクエストの追加に失敗しました: \(error.localizedDescription)")
            }

終わりに

最小実装なので全部ViewControllerに書きました。
通知許可を求めるところなんかはアプリ起動時とかにアラートがでるアプリが多そうですね!
間違ってたら教えて下さい!

参考

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2