簡體   English   中英

Swift 4:一次設置本地通知

[英]Swift 4 : Set local notification once

我想為特定時間設置本地通知,但對此有所擔憂。 例如,當我一次設置本地通知並且說我安排了該通知,突然用戶的電話關閉時,會發生什么? 我確定此通知數據僅在內存中可用,並且在用戶電話關閉時將消失。

而且我知道每次設置相同的日期時,我都會收到更多通知。 我只想要一個通知,但是正如我告訴您的那樣,我不知道如何為用戶處理各種可能性。

這是我的示例代碼:

func scheduleNotification(inSeconds : TimeInterval, completion : @escaping (_ Success : Bool) -> ()){
    let notif = UNMutableNotificationContent()
    notif.title = "New notification"
    notif.subtitle = "Cool!"
    notif.body = "I liked it!!!!"

    let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: inSeconds, repeats: false)
    let request = UNNotificationRequest(identifier: "myNotification", content:  notif, trigger: notifTrigger)

    UNUserNotificationCenter.current().add(request) { (error) in
        if error != nil{
            print(error!)
            completion(false)
        }else{
            completion(true)
        }
    }
}

本地通知存儲在會話之間。 我只是通過設置一個,然后再打開和關閉手機來進行測試。 通知仍然出現。 當然,如果手機關機,您也不會收到通知。

暫無
暫無

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

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