I'm wondering how I can make a local notification in swift repeat on the first of every month. So on the first of January, reminder. On the first of February, same reminder, and so on. Preferably using date components.
Asked
Active
Viewed 1,022 times
1 Answers
1
- First we need to get default "UNUserNotificationCenter".
- Create "UNNotificationContent".
- Create the "UNCalendarNotificationTrigger".
- Create "UNNotificationRequest".
- Add "UNNotificationRequest" to "UNUserNotificationCenter".
let center = UNUserNotificationCenter.current()
//create the content for the notification
let content = UNMutableNotificationContent()
content.title = " Title"
content.subtitle = "SubTitle"
content.body = "jvsvsvasvbasbvfasfv"
content.sound = UNNotificationSound.default
var dateComp = DateComponents()
dateComp.month = 1;
dateComp.day = 1;
dateComp.hour = 00;
dateComp.minute = 00;
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComp, repeats: true)
//create request to display
let request = UNNotificationRequest(identifier: "ContentIdentifier", content: content, trigger: trigger)
//add request to notification center
center.add(request) { (error) in
if error != nil {
print("error \(String(describing: error))")
}
}
Use can refer this link more info.

Shreeram Bhat
- 2,849
- 2
- 11
- 19
-
@EvanC repeats flag is set to true in UNCalendarNotificationTrigger right?. So it should repeat I guess. You can test this by manually changing date in iPhone. – Shreeram Bhat Sep 10 '19 at 04:55