0

I used the following code to trigger a daily reminder:

let triggerDate = calendar.date(from: calendarComponents)
let triggerDaily = Calendar.current.dateComponents([.hour, .minute, .second], from: triggerDate!)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let request = UNNotificationRequest(identifier: "daily-identifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
//UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifier])
UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
      if error != nil {
        debugPrint("center.add(request, withCompletionHandler: { (error)")
      } else {
        debugPrint("no error?! at request added place")
      }
    })

The above code allows me to set a daily alarm with identifier daily-identifier. Since I plan to set two daily alarm in my code, I used the above code and use another identifier second-daily-identifier to trigger another alarm with different hour/minutes combination.

Problem I encountered: If I use the code

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

before adding the request, it will remove the alarm that set earlier (e.g., if i add "daily-identifier" alarm first, then when i add "second-daily-identifier" will remove the "daily-idenifier" alarm.

If i use the other method

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifier])

to specifically remove the one I am going to set, then it works well. However, I found that I can never stop it any more. e.g., if I have change the identifier to "third-identifier", and then the already set alarm will be in my phone forever. and I cannot remove them even I remove the app.

What have I done wrong?

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
user6539552
  • 1,331
  • 2
  • 19
  • 39
  • Yes even if you uninstall then re-install that notification will still exist with the previously set identifier. So just only change Identifiers when you have removed all the existing notifications with the matching identifier. It can be kinda a pain, apple should have it just remove all notifications. – zsteed Jul 19 '17 at 15:00
  • I really surprised that after i removed the app, it is still here! – user6539552 Jul 19 '17 at 15:50

1 Answers1

0

For cancelling all pending notifications, you can use this:

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

For cancelling specific notifications,

UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
   var identifiers: [String] = []
   for notification:UNNotificationRequest in notificationRequests {
       if notification.identifier == "identifierCancel" {
          identifiers.append(notification.identifier)
       }
   }
   UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
}
Kendrick
  • 1
  • 2