Find list of Local Notification the app has already set

后端 未结 8 1854
盖世英雄少女心
盖世英雄少女心 2020-12-01 05:37

My app allows users to set a number of reminders in the future. When the app lauches I want to know what reminders (notifications) have already been set.

Can I read

8条回答
  •  伪装坚强ぢ
    2020-12-01 06:02

    Swift 4

    UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { (requests) in
    
        for request in requests {
    
            if request.identifier == "IDENTIFIER YOU'RE CHECKING IF EXISTS" {
    
                //Notification already exists. Do stuff.
    
            } else if request === requests.last {
    
                //All requests have already been checked and notification with identifier wasn't found. Do stuff.
    
            }
        }
    })
    

    I used this to fix a bug where the same weekly notification was already set and being set again when the app would open, so it would keep resetting the timer to appear, which means it never did appear.

提交回复
热议问题