Learn Courses My Dashboard

How to activate code every day at midnight

I want to activate some code every day at midnight to update my icon badge. While researching this, I saw suggestions for using a timer and also using the Notification Center. I tried the Notification Center suggestion, but get various Xcode errors. Also, I couldn’t tell if I had to run the activation code every day or would it repeat automatically.

Any suggestions?

I’m not sure why you want to update the badge every night at midnight, but you could try push notifications from Firebase.

So really code is coming from the server (Firebase) at a specified time

Otherwise there is way to run code in the background (if the user has the app terminated) but I don’t think you can schedule it to run at a certain time

And midnight for who??? Your users may be on different time zones

My data includes a due date for bills. The badge indicates the number of items that have reached or passed their due date. So at midnight (local time), the badge might have to be updated.

Here’s a good starting point!

I think the correct way to handle this would be to set a local notification for an item’s due date using UNUserNotificationCenter and UNCalendarNotificationTrigger and then let the system handle updating the badge when the notification fires off.

I’m already using the Notification Center to send a notification. How to I use it to update the badge?