batoulapps / adhan-swift

High precision Islamic prayer time library for Swift
MIT License
182 stars 42 forks source link

Setting Multiple Local Notifications #85

Closed sabrefm1 closed 1 year ago

sabrefm1 commented 1 year ago

Hi, so I am trying to set notifications for all 5 prayers of the day, as a test I am able to schedule a single prayer using the code below. Prayer for fajr time will send a notification. How would I go about scheduling it for the remaining 4. Is it possible to add multiple triggers?

let cal = Calendar(identifier: Calendar.Identifier.gregorian)
            let date = cal.dateComponents([.year, .month, .day], from: Date())
            let coordinates = Coordinates("")
            var params = CalculationMethod.other.params
            params.madhab = .shafi

            if let prayers = PrayerTimes(coordinates: coordinates, date: date, calculationParameters: params) {
                let formatter = DateFormatter()
                formatter.timeStyle = .medium
                formatter.timeZone = TimeZone(identifier: "timezone")!

                let notificationContent = UNMutableNotificationContent()

                notificationContent.title = "Prayer"
                notificationContent.sound  = UNNotificationSound.default

                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: prayers.fajr.timeIntervalSinceNow, repeats: false)

                let request = UNNotificationRequest(identifier: "prayerAlert", content: notificationContent, trigger: trigger)

                UNUserNotificationCenter.current().delegate = self

                UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
basememara commented 1 year ago

Salaam, although kinda out of scope of this library I can share what I've done for my own app. My code wraps Adhan so it is using this library as a dependency. I hope it helps: https://github.com/ZamzamInc/PrayKit/blob/main/Sources/PrayServices/Notification/NotificationServiceUN.swift

z3bi commented 1 year ago

@sabrefm1 you will need to create a different UNNotificationRequest for each prayer notification you want.