UserNotifications
Document
https://developer.apple.com/documentation/usernotifications
ローカル通知を毎週通知する
Example 毎週月曜日 7:15
let content = UNMutableNotificationContent()
content.title = "Example"
content.body = "毎週月曜日 7:15"
content.sound = UNNotificationSound.default
let notificationCenter = UNUserNotificationCenter.current()
var dateComponentsDay = DateComponents()
dateComponentsDay.hour = 7
dateComponentsDay.minute = 15
dateComponentsDay.weekday = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponentsDay, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
notificationCenter.add(request) { (error) in
if error != nil {
print(error.debugDescription)
}
}
// 登録されている通知確認
UNUserNotificationCenter.current().getPendingNotificationRequests {
print("Pending requests :", $0)
}
DateComponentsの年月日等の余計な情報は設定しない。
DateComponentsのweekdayに通知したい曜日を設定。
weekdayの設定値
1:日曜日
2:月曜日
3:火曜日
4:水曜日
5:木曜日
6:金曜日
7:土曜日