repeating local notifications

Hi all,

I am having trouble making my app. I am making an app that configures notifications on a save button and many of these notifications are on a repeat interval of a day. I would like to, after a certain amount of time e.g. when an object reaches a certain age, change the repeat interval to every other day. How would I go about doing this? I would love to get some help on this.

Thanks,

Iguana321

Replies

I would like to, after a certain amount of time e.g. when an object reaches a certain age, change the repeat interval to every other day. How would I go about doing this?

It’s not possible to do this directly. There are two issues:

  • UILocalNotification has

    repeatInterval
    and
    repeatCalendar
    properties but it has no way to bound the repeats. So you can’t, for example, schedule notification A with X repeats every day and then notification B with Y repeats every other day.
  • Even if the above were possible, UILocalNotification has no way to specify “repeat every other day”.

Your only option would be to calculate the times that you want each of the repeats to fire and then schedule non-repeating notifications for each one. There’s two gotchas with this:

  • Calculating dates into the future is tricky. NSCalendar will do the work, but you have to be very careful how you use it. It’s very easy to introduce subtle bugs that only crop up in odd circumstances (like daylight saving time changes).

  • There’s a relatively low limit to how many local notifications you can schedule (last I checked it was 64), which puts a bound on how effective this is.

Finally, depending on the nature of your product, you could change tack and put the events in the user’s calendar. EventKit has a lot more flexibility when it comes to repeating events.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi Eskimo,

Thank you for replying so soon. Also thank you for putting me out of my misery as I have been searching for hours on the internet looking for a solution where there is none. I am an amature app developer and have had some experience with scheduling notifications and repeating them and (if I am correct) you can schedule a notification on certain days of the week and then repeat them weekly. I am new to this and if this is a possibility could you possibly help me out?

Thanks again,

Iguana321

Hi Eskimo,

I have been thinking. My solution to this problem is that in the app delegate when it recieves a notification I am checking whether the notification is the same as the one I specify and then I add 1 day onto the fire date. As follows:

for element in notifs{

if notification == element.notification{

element.notification.firedate = calendar.dateByAddingUnit(.Day, Value: 1, toDate: dateFire!, options: NSCalendarOptions())

}

}

this code is in the didRecieveLocalNotification function in the AppDelegate. Please could you tell me if this won't work.

Thank you once again for your help,

Iguana321

It can only work if the user opens the notification. If the user doesn't tap on it, no code is run In your app.

Hi junkpile,

Thank you for replyng. This just makes things worse. Is there a way of running this code even if the user does not click on the notification? If not, is there a way of running code every frame to do something similar to this like the Update function in C-sharp?

Thanks again,

Iguana321

No. If the user never opens a notification and never opens your app, then none of your app code will ever run. Those two cases are the only opportunities you have to reschedule future local notifications.


Have you considered using remote notifications? That way your server could control the timing of delivery as far into the future as you want. Of course that requires network access for notifications to be delivered, but that's not an issue for most use cases.

Thank you for your reply. I don't think I can use remote notifications as the user needs to be able to recieve notifications while not connected to a network. Just as feedback. If you were my user and had a notification relying on a date of birth would it matter if a notification only changes when you enter the application or tap on the notification? It would change if the user reaches a certain age using the date of birth. Could another alternative be to schedule a function to happen on a certain date if that is possible?

Thank you for all of your help,

Iguana321