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!
https://www.hackingwithswift.com/example-code/system/how-to-run-code-when-your-app-is-terminated

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 understand now. I didn’t realize that one of the options for local notifications is badge (meaning badge number). It’s working now. Thanks.