簡體   English   中英

無法通過 UNUserNotificationCenter 獲得重復的每日通知以顯示

[英]Unable to get repeating daily notification to show with UNUserNotificationCenter

我的目標是設置一個重復的每日提醒通知。 盡管在代碼中設置了repeat: true ,但以下代碼始終成功顯示通知一次,但不重復它。

整個通知代碼如下:

    let notificationID = "daily_sleep_alarm"
    let title = "Rest your weary eyes"
    let body = "It's bedtime. The comfort of your blanket beckons you."

    UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [notificationID])
    
    let center = UNUserNotificationCenter.current()
    let content = UNMutableNotificationContent()
    content.title = title
    content.body = body
    content.categoryIdentifier = "daily_sleep_alarm"
    content.userInfo = ["customData": "fizzbuzz"]
    content.sound = nil

    var dateComponents = DateComponents()
    dateComponents.hour = hourSelected
    dateComponents.minute = minuteSelected
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
    let request = UNNotificationRequest(identifier: notificationID, content: content, trigger: trigger)
    center.add(request)

如上所述,此代碼一次后無法成功顯示通知。 我沒有其他代碼可以清除具有此特定notificationID的待處理通知。

在應用程序啟動時,我還會檢查center.getPendingNotificationRequests有哪些待處理的通知,它總是顯示為無。

任何關於我需要做什么來正確設置每日重復通知的見解都非常感謝。

我只是每天使用這些簡單的代碼行

    func setNotifications(with interval: TimeInterval) {
    let content = UNMutableNotificationContent()
    content.title = "Tittle"
    content.subtitle = "Subtle"
    content.body = "Don't forget yourself!"
    content.badge = 1
    
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval,
                                                    repeats: true)
    
    let requestIdentifier = "demoNotification"
    let request = UNNotificationRequest(identifier: requestIdentifier,
                                        content: content, trigger: trigger)
    
    UNUserNotificationCenter.current().add(request,
                                           withCompletionHandler: { (error) in
                                            // Handle error
    })
}

暫無
暫無

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

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