简体   繁体   中英

Unable to get repeating daily notification to show with UNUserNotificationCenter

My goal is to set up a repeating daily reminder notification. The following code always successfully show the notification once , but does not repeat it, despite setting repeat: true in the code.

The entire notification code is below:

    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)

As noted above, this code does not successfully show the notification after once. I have no other code that clears pending notifications with this particular notificationID .

On app start, I also check to see what pending notifications are available with center.getPendingNotificationRequests , and it always shows as none.

Any insights on what I need to do to correctly set up the daily repeating notification is muched appreciated.

i'm just using this simple lines of code for daily

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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