batoulapps / adhan-swift

High precision Islamic prayer time library for Swift
MIT License
185 stars 45 forks source link

Unable to get notifications to work :/ #77

Closed tryingtobegoodatcoding closed 1 year ago

tryingtobegoodatcoding commented 2 years ago

` func calculateTime()->Void{ let date = cal.dateComponents([.year, .month, .day], from: Date()) var params = CalculationMethod.moonsightingCommittee.params params.madhab = .hanafi params.adjustments.dhuhr = -1 params.adjustments.asr = 3 params.adjustments.maghrib = -1 params.adjustments.isha = 2 if let prayers = PrayerTimes(coordinates: coordinates, date: date, calculationParameters: params) { let formatter = DateFormatter() formatter.timeStyle = .short formatter.timeZone = TimeZone(identifier: "Europe/London")!

        self.sunrise = formatter.string(from: prayers.sunrise)
        self.fajr = formatter.string(from: prayers.fajr)
        self.zuhr = formatter.string(from: prayers.dhuhr)
        self.asr = formatter.string(from: prayers.asr)
        self.maghrib = formatter.string(from: prayers.maghrib)
        self.isha = formatter.string(from: prayers.isha)
        if let currentPrayer = prayers.currentPrayer() {
            self.current = formatter.string(from: prayers.time(for: currentPrayer))
        } else {
            // if there's no current prayer then it means its between midnight and fajr.
            // It's up to you to decide how you want to handle this situation
            self.current = formatter.string(from: prayers.fajr)
        }
        if let nextPrayer = prayers.nextPrayer() {
            self.next = formatter.string(from: prayers.time(for: nextPrayer))
        } else {
            // if there's no current prayer then it means its between midnight and fajr.
            // It's up to you to decide how you want to handle this situation
            self.next = formatter.string(from: prayers.fajr)
        }

    }
    func noonNotify() {
        let center = UNUserNotificationCenter.current()
        let content = UNMutableNotificationContent()
        content.title = "prayer time"
        content.body = "Time to pray"
        content.categoryIdentifier = "alarm"
        content.sound = UNNotificationSound.default

        let trigger = UNCalendarNotificationTrigger(dateMatching: PrayerTimes(coordinates: coordinates, date: now).next, repeats: false)

        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        center.add(request)
    }
}

`

This is the date so far but I am unable to get past "cannot convert value of type 'Date' to expected argument type 'DateComponents'"

Please any help appreciate, have a project due soon :(

pernebayevz commented 2 years ago

let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: yourDate) let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

You have to convert your nextPrayer Date into DateComponents