0

hello i want to send notification like daily reminder even the app terminated not working in background how i can do it and this my code

func soundNotification(){

    let prayers = ["fajer","dohor","asr","maghreb","isha"]

    let content = UNMutableNotificationContent()
    content.title = "\(prayers[indexOfNextPrayer + 1]) azan will be after 5 minutes"
    content.badge = 1
    content.sound = UNNotificationSound(named: "salah.mp3")
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: "azanSoon", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

}
  • 1
    I guess you need to use `let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)` method. Pass the date components. – TheTiger May 17 '18 at 05:26
  • As commented above, use the `UNCalendarNotificationTrigger` instead. It’s allows you to create daily notifications. – Mannopson May 17 '18 at 07:04
  • Possible duplicate of [Sending local notifications after the app has been terminated](https://stackoverflow.com/questions/32271528/sending-local-notifications-after-the-app-has-been-terminated) – Scriptable May 17 '18 at 07:36

1 Answers1

1
func soundNotification(){
    let prayers = ["fajer","dohor","asr","maghreb","isha"]
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let content = UNMutableNotificationContent()
    content.title = "\(prayers[indexOfNextPrayer + 1]) azan will be after 5 minutes"
    content.body = " "
    content.badge = 1
    content.sound = UNNotificationSound(named: "salah.mp3")
    let request = UNNotificationRequest(identifier: "azanSoon", content: content, trigger: trigger)
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().add(request) {(error) in
        if let error = error {
            print("error: \(error)")
        }
    }
}

Trigger local Notification at date and time.

 func scheduleNotification(at date: Date, message:String) {
    let calendar = Calendar(identifier: .gregorian)
    let components = calendar.dateComponents(in: .current, from: date)
    let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
    let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
    let content = UNMutableNotificationContent()
    content.title = "Reminder"
    content.body = message
    content.sound = UNNotificationSound.default()
    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().add(request) {(error) in
        if let error = error {
            print("error: \(error)")
        }
    }
}