簡體   English   中英

立即使用UNUserNotificationCenter調用本地通知

[英]Invoking a local notification with UNUserNotificationCenter right now

我現在如何調用本地通知? 而不是設置將來的時間間隔? 此代碼將在未來5秒鍾顯示通知。 我希望現在就調用它。

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5.0, repeats: false)

let notificationRequest = UNNotificationRequest(identifier: "notification", content: notificationContent, trigger: trigger)

UNUserNotificationCenter.current().add(notificationRequest) { (error) in
    if let error = error {
        //error
    }
}

由於timeInterval必須大於0,因此您可以嘗試將0.1放入timeInterval。

或者,您可以在特定時間觸發本地通知。(現在時間)

let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.timeZone], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM