
iOSアプリでローカル通知を送信したいときに必要なのが UNNotificationRequest です。
- 「アプリを閉じていても決まった時間にリマインダーを送りたい」
- 「タスクの期限前に通知したい」
といったニーズを簡単に実現できます。
この記事では UNNotificationRequest の 基本的な意味、使い方、主要な引数、よくある活用シーン を初心者向けにわかりやすく解説します。
UNNotificationRequest とは?
UNNotificationRequest は UserNotifications フレームワークの中で、通知をシステムに依頼するためのクラス。
iOSアプリが「この内容の通知を、このタイミングで送ってください」とシステムにお願いするためのオブジェクトです。
簡単に言うと:
- 何を通知するか(タイトル、メッセージ)
- いつ通知するか(5分後、毎日午前9時など)
- どの通知か識別するID
これらをまとめて一つのオブジェクトにし、iOSの通知システムに登録します。
つまり「どんな通知を、いつ送るか」を具体的に指定するための“依頼書”のような存在です。
基本的な使い方
まずは基本の例を見てみましょう。
以下のコードは、通知の許可を取得したあとで、ボタンを押してから 5秒後に通知 を配信するものです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import SwiftUI import UserNotifications struct ContentView: View { @State private var message = "" var body: some View { VStack(spacing: 20) { Text("通知のテスト") .font(.title) Button("5秒後に通知") { send5SecondNotification() } Text(message) .foregroundColor(.blue) } .padding() .onAppear { // アプリ起動時に通知許可を求める requestPermission() } } // 通知許可を求める private func requestPermission() { UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in // 許可の結果は無視(実際のアプリでは適切に処理) } } // 5秒後に通知を送る private func send5SecondNotification() { // ① 通知の内容を作成 let content = UNMutableNotificationContent() content.title = "お知らせ" content.body = "5秒経ちました!" content.sound = UNNotificationSound.default // ② いつ送るかを指定(5秒後、繰り返しなし) let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) // ③ UNNotificationRequestを作成 let request = UNNotificationRequest( identifier: "test-notification", // 識別ID content: content, // 通知内容 trigger: trigger // 送信タイミング ) // ④ 通知をスケジュール UNUserNotificationCenter.current().add(request) { error in DispatchQueue.main.async { if error != nil { self.message = "通知の設定に失敗しました" } else { self.message = "5秒後に通知します!" } } } } } |
この例では content(通知内容) と trigger(タイミング) を指定し、それらをまとめて UNNotificationRequest として通知センターに登録しています。
主要な引数の意味
UNNotificationRequest のイニシャライザは次の形です。
1 |
UNNotificationRequest(identifier: String, content: UNNotificationContent, trigger: UNNotificationTrigger?) |
1. identifier(識別ID)
identifier では、通知を識別するIDを指定します。
1 2 |
identifier: "test-notification" |
- 役割: 通知を区別するためのユニークなID
- 使い道: 同じIDで新しい通知を作ると、古い通知が上書きされる
- 例: "daily-reminder", "task-123", "workout-notification"
2. content(通知内容)
content では、通知の本文やタイトル、音などの内容を指定します。
1 2 3 4 5 6 |
let content = UNMutableNotificationContent() content.title = "タイトル" // 通知のタイトル content.body = "メッセージ" // 通知の本文 content.sound = .default // 通知音 content.badge = 1 // アプリアイコンに表示される数字 |
主なプロパティ:
プロパティ | 説明 | 例 |
---|---|---|
title |
通知のタイトル | "リマインダー" |
body |
通知のメッセージ | "会議が10分後に始まります" |
sound |
通知音 | .default (デフォルト音) |
badge |
アプリアイコンの数字 | 1 (赤い丸に"1"が表示) |
3. trigger(送信タイミング)
triggerは通知をいつ送るかを決める部分です。
主に下記3種類あります:
① 〜時間後に送る(UNTimeIntervalNotificationTrigger)
1 2 3 4 5 6 |
// 30分後に1回だけ let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 30 * 60, repeats: false) // 24時間ごとに繰り返し let repeatingTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 24 * 60 * 60, repeats: true) |
② 特定の日時に送る(UNCalendarNotificationTrigger)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// 毎日午後3時に通知 var dateComponents = DateComponents() dateComponents.hour = 15 dateComponents.minute = 0 let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) // 特定の日付の1回だけ var specificDate = DateComponents() specificDate.year = 2024 specificDate.month = 12 specificDate.day = 25 specificDate.hour = 10 let onTimeTrigger = UNCalendarNotificationTrigger(dateMatching: specificDate, repeats: false) |
特定の場所で送る(UNLocationNotificationTrigger)
1 2 3 4 5 6 7 8 9 |
import CoreLocation // 会社から半径100m以内に入ったときに通知 let center = CLLocationCoordinate2D(latitude: 35.6762, longitude: 139.6503) let region = CLCircularRegion(center: center, radius: 100, identifier: "office") region.notifyOnEntry = true // 入った時に通知 region.notifyOnExit = false // 出た時は通知しない let trigger = UNLocationNotificationTrigger(region: region, repeats: false) |
よくある活用シーン
UNNotificationRequest は以下のようなアプリで活用されます。
- タスク管理アプリ:締め切りの通知
- フィットネスアプリ:運動時間をリマインド
- 学習アプリ:復習タイミングの通知
- ECアプリ:セールやカート放置の通知
- カレンダーアプリ:イベント開始前のリマインド
ユーザーがアプリを開いていないときでも適切に通知できるため、アプリ利用を継続してもらう強力な手段になります。
注意点
便利な UNNotificationRequest ですが、使う際の注意点もあります。
- 通知許可が必須:ユーザーが拒否すると通知は届かない
- 同じ identifier の通知は上書きされる
- 通知は最大64件まで(古いものから消える)
- 位置ベース通知は追加の権限が必要
- 添付ファイルにはサイズ制限(画像10MB、動画50MBなど)
これらを理解した上で設計することが大切です。
まとめ
今回は UNNotificationRequest の基本を解説しました。
- 通知を登録するための中心クラス
identifier
、content
、trigger
の組み合わせで通知を定義- 時間・日付・位置に応じた多様な通知が可能
- リマインダーやイベント通知など幅広いアプリで活躍
- 設計時には許可管理や件数制限に注意
UNNotificationRequest をマスターすれば、ユーザーに寄り添う便利な通知機能を簡単に実装できます。
ぜひアプリ開発に活用してみてください!