1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift ローカル通知を毎週通知する

Posted at

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:土曜日

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?