iostimecalendarusernotifications

Creating UNNotification with non Gregorian Calendar


I’m building my App to fire daily, weekly and monthly notifications in IOS swift 4. The notifications work perfectly using Gregorian calendar. However, when I change the date to Hijri calendar (Calendar(identifier: .islamicUmmAlQura) it does not wrk. It seams the UNCalendarNotificationTrigger converts any date to Gregorian calendar. Below monthly notification code works perfectly when using Gregorian calendar:

func myNotification(at date: Date, withTitle title:String, andBody body:String, notificationIdentifier:String) {

let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents([.day,.hour, .minute], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.init(named: "notificationSound.wav")
let request = UNNotificationRequest(identifier: notificationIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [notificationIdentifier])
UNUserNotificationCenter.current().add(request) {(error) in
    if let error = error {
        print(" error: \(error)")
    }
}

}

The below code does not work when I covert the date to Islamic date using (Calendar(identifier: .islamicUmmAlQura) :

func myNotification(at date: Date, withTitle title:String, andBody body:String, notificationIdentifier:String) {

let calendar = Calendar(identifier: .islamicUmmAlQura)
let components = calendar.dateComponents([.day,.hour, .minute], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.init(named: "notificationSound.wav")
let request = UNNotificationRequest(identifier: notificationIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [notificationIdentifier])
UNUserNotificationCenter.current().add(request) {(error) in
    if let error = error {
        print(" error: \(error)")
    }
}

Solution

  • It worked after adding the calendar type to the trigger

    trigger = UNCalendarNotificationTrigger(calendar:calendar, dateMatching: components, repeats: true)