I’m trying to create a daily local notification as a reminder for the user to take a particular action. If the user has already taken that action on the current day, prior to the scheduled notification time, I want the reminder for that day to be skipped (cancelled), but the daily notifications should be resumed the following day.
I have scoured the API docs, the forums, and Google, and have not found an easy and reliable way to do this. If I call
cancelScheduledNotificationAsync(), it cancels the entire series of daily notifications. I only want to cancel/skip the next one, but leave the series intact for the following days. Anyone have any ideas?
Well, I came up with a bit of a hack to accomplish this. Since there doesn’t seem to be any officially supported way to do it (and there certainly should be), it seems that a hack will have to do.
My approach was this:
- Schedule a repeating weekly notification for each day of the week.
- When the user takes the action in question (in my example, it’s when they view a particular screen, but it could be any action that makes sense in your app) and they do so before the time when the notification is scheduled for that day, I cancel the weekly notification for the current day of the week.
- Every time they open the app, a function will run to check the weekly notifications and replace any that have been cancelled. So, if the weekly notification for Mondays got cancelled on Monday morning, it will be recreated on Tuesday (or whichever day the user next uses the app).
- There is a risk that the user might not use the app again for more than a week, which means that they would not get the notification the following Monday. But at most, they would only miss getting a daily notification on one day of the week. Since the function to recreate the missing (cancelled) notifications runs every time the user opens the app, there is no way that notifications could be cancelled for more than one day of the week. In my case, I considered that to be an acceptable risk.
I considered another approach. For anyone reading this who is facing a similar problem, this might or might not work better for you than the above solution:
- Instead of weekly notifications for each day of the week, you could try creating a series of non-repeating notifications that are scheduled for the same time each day, for a fixed number of days in the future (maybe 30 days, for example).
- Every time the user opens your app, you can run a function which manages these notifications, adding additional ones onto the end of your rolling n-day period as necessary.
- When the user takes the action that triggers cancelation for the current day, none of the other notifications will be affected.
- In this case, your risk is that the user will not use your app for the number of days for which you have scheduled notifications, after which they will stop receiving the notifications. If you make this a long enough number of days in the future, it becomes less of a risk. And if the user stops using your app for more than 30 days, you might have lost them anyway.
Anyway, these are some ideas for how to work around what seems to me like a missing part of the API. If anyone has any additional ideas, I’d love to hear them.