简体   繁体   English

快速显示本地通知,即使应用终止

[英]swift display local notification even the app terminated

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)

}
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)")
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM