I'm building a reminders app that sends local notifications that repeat based on a user's custom settings. For example, I may send a notification every other day at 8pm. Every time I send one of these notifications, I'd like to replace any previous notification that might be displayed in the user's notification center already.
The new UserNotifications framework in iOS gets me close to being able to do this. As far as I can tell, notifications can be replaced in two ways:
- Use a
UNCalendarNotificationTrigger
withrepeats: true
. Repeated triggers replace past ones. - Create a new
UNNotificationRequest
with the sameidentifier
. Whenever a request is sent with the sameidentifier
, it replaces all other instances.
Neither of these solutions quite work for me:
- I can't use use repeating notifications because
UNCalendarNotificationTrigger
usesDateComponents
for a repeating schedule which just isn't granular enough. My example above of "every other day at 8pm" can't be described with justDateComponents
. - Because I need to schedule several notifications all at once, I must specify unique
identifier
s, and as such, they don't replace each other when displayed.
I was hoping service extensions would save me here. If I could execute code when I receive a notification, I could programmatically remove old notifications when new ones are triggered. Unfortunately, it appears as if service extensions only work for remote notifications. Even if I could use remote notifications (which is a whole can of worms by itself), this solution kind of bends the rules on what is allowed for service extensions.
Does anyone have any ideas on how to solve this, or am I destined to spam my users' notification centers?