Elliot
Elliot

Reputation: 6136

How can I set an expiration time for a UILocalNotification?

I would like to show a UILocalNotification, but it will only be valid for 24 hours. At the end of 24 hours, I want the notification to "expire"; that is: the user can no longer see the obsolete notification. How can I do this?

Upvotes: 2

Views: 2348

Answers (3)

Vitalii
Vitalii

Reputation: 4437

To cancel already delivered notifications use

UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["myidentifier"])

or

UNUserNotificationCenter.current().removeAllDeliveredNotifications()

Example code in this answer: https://stackoverflow.com/a/45218609/2650588

Upvotes: 2

Elliot
Elliot

Reputation: 6136

I'm referring to a notification that has already fired.

I believe that setting the badge to zero will clear out existing notifications. I will try to see whether this also clears local notifications (in addition to push notifications).

Upvotes: 1

onnoweb
onnoweb

Reputation: 3038

You can use UIApplication.scheduledLocalNotifications to get a list of your pending notifications and then cancel the ones you want to get rid of.

Upvotes: 0

Related Questions