0

I have this code that should send a notification to the user on the same day every month. However I'm can't simulate this, so I can't test it. I was wondering if any of you could proof check it, to ensure it does send a notification every month. Thanks

static func addNotification(){
    let center = UNUserNotificationCenter.current()
    
    let addRequest = {
        let content = UNMutableNotificationContent()
        content.title = "Test"
        content.subtitle = "Test"
        content.sound = UNNotificationSound.default
        
        var dateComponents = DateComponents()
        dateComponents.day = 31
        dateComponents.hour = 12

        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
        
        let request = UNNotificationRequest(identifier: "", content: content, trigger: trigger)
        center.add(request)
    }
    
    center.getNotificationSettings { settings in
        if settings.authorizationStatus == .authorized{
            addRequest()
        }
        else{
            center.requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
                if success{
                    addRequest()
                }
            }
        }
    }
}
jnpdx
  • 45,847
  • 6
  • 64
  • 94
Cian B
  • 31
  • 6
  • 2
    I don´t know much about `xcode`, but it seems to me that not all months have 31 days (`dateComponents.day = 31`) – nacho Dec 29 '22 at 10:47
  • See here how to get the last day of a month: https://stackoverflow.com/questions/33605816/first-and-last-day-of-the-current-month-in-swift – koen Dec 29 '22 at 11:43

0 Answers0