ローカル通知で「特定の日時に通知を届けたい」というときに便利なのが UNCalendarNotificationTrigger
です。
たとえば「毎日朝9時に通知」「特定の日の20時にアラート」など、カレンダーに基づくタイミングで通知をスケジュールするためのクラスです。
この記事では UNCalendarNotificationTrigger
の基本的な意味や使い方、主要な引数の意味、活用シーン、注意点までをわかりやすく丁寧に解説します。
UNCalendarNotificationTriggerとは?
UNCalendarNotificationTrigger
は、ユーザー通知フレームワーク(UserNotifications.framework)に属するクラス。
日付や時間などのカレンダー情報をもとに通知を発火させるためのトリガーです。
たとえば次のような使い方ができます。
- 2025年12月24日 20:00 に通知
- 毎日朝8時に通知(繰り返し)
- 毎週月曜の15時に通知
このクラスは iOS 10 以降で利用でき、リマインダー系アプリや定期通知機能を実装するときによく使われます。
具体例:毎日9時に通知を送る
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 |
import UserNotifications let center = UNUserNotificationCenter.current() // 通知の内容 let content = UNMutableNotificationContent() content.title = "おはようございます" content.body = "今日も一日がんばりましょう!" content.sound = .default // 毎日9:00に発火するトリガーを作成 var dateComponents = DateComponents() dateComponents.hour = 9 let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) // 通知リクエストを作成 let request = UNNotificationRequest(identifier: "morningNotification", content: content, trigger: trigger) // 通知を登録 center.add(request) { error in if let error = error { print("通知の登録に失敗: \(error.localizedDescription)") } } |
重要なのは、DateComponents
を通じて「何時・何分」に通知を鳴らすかを指定している点です。
指定できる引数とその意味
UNCalendarNotificationTrigger
は次のようなイニシャライザを使って作成します。
1 2 |
UNCalendarNotificationTrigger(dateMatching: DateComponents, repeats: Bool) |
それぞれの引数の意味は以下の通りです。
引数名 | 型 | 説明 |
---|---|---|
dateMatching | DateComponents | 通知を発火させたい日時を表すカレンダー情報。 年、月、日、曜日、時、分、秒などの要素を組み合わせて指定する。 |
repeats | Bool | 通知を繰り返すかどうか。trueにすると毎日・毎週などの繰り返し通知にできる。 |
具体的な指定方法の例
① 毎日9時に通知
「毎朝9時」という繰り返し通知を設定するには、時だけを指定し、repeats を true にします。
1 2 3 4 |
var components = DateComponents() components.hour = 9 let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true) |
→ 年・月・日を指定していないので「毎日朝9時」という繰り返し条件になります。
② 毎週月曜日の10時に通知
「毎週決まった曜日・時間」に通知したいときは、曜日と時刻を指定します。
weekday = 2 は日曜=1、月曜=2 というルールです。
1 2 3 4 5 |
var components = DateComponents() components.weekday = 2 // 月曜日 components.hour = 10 let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true) |
→ 毎週月曜の10時に通知されます。
③ 特定の日付・時間に1回だけ通知
「クリスマスイブの夜8時に通知」など、一度きりの通知を設定する場合は、年月日と時刻をすべて指定し、repeats を false にします。
1 2 3 4 5 6 7 |
var components = DateComponents() components.year = 2025 components.month = 12 components.day = 24 components.hour = 20 let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false) |
→ 2025年12月24日20時に1回だけ通知されます。
④ 毎月1日の9時に通知
「毎月初日の朝9時」というような指定も可能です。
1 2 3 4 5 |
var components = DateComponents() components.day = 1 components.hour = 9 let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true) |
→ すべての月の「1日・9時」に通知されます。
UNCalendarNotificationTrigger の活用シーン
UNCalendarNotificationTrigger
は次のような場面で活用されます。
- 朝や夜など、特定の時間帯にルーチンを通知する
- 誕生日や記念日などの1年に1度の通知
- 毎週決まった曜日に習慣づけのためのリマインダー
- イベントの前日や当日にアラートを出す
- 日付指定で重要タスクのリマインドをする
時間指定の通知を簡単に実装できるので、ヘルスケア、日記、勉強アプリなど幅広いジャンルで使われています。
使用時の注意点
便利な UNCalendarNotificationTrigger
ですが、使う上での注意点もあります。
- ユーザーの許可が必要:通知を使うには
UNUserNotificationCenter
にリクエストして、許可を得る必要があります。 - repeats = true の場合は完全な日付は指定不可:
repeats
を true にする場合、年や月などのすべての要素を含めると正しく動作しません。- 例:
year, month, day, hour
をすべて指定 + repeats: true → 無効になる可能性あり
- 例:
- 通知はアプリがバックグラウンドでも動作する:ただしスケジュール数には制限(最大64件)があります。
- トリガーを削除しないと通知が残る:キャンセルしたい場合は、
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers:)
を使って削除が必要です。
まとめ
今回は UNCalendarNotificationTrigger
について詳しく紹介しました。
- カレンダーに基づく日時で通知を発火できるトリガー
DateComponents
により柔軟な時間指定が可能- 毎日/毎週などの繰り返し通知も簡単に設定できる
- iOS 10 以降で利用可能な UserNotifications.framework の一部
通知のタイミングをカレンダー形式で細かく制御したいときは、UNCalendarNotificationTrigger
をぜひ活用してみてください。
アプリでローカル通知を実装したい時などにすごく便利です!