Wrap up
Wrap up êŽë š
That was easy, right? And yet it's such a great feature to have, because now your app can talk to users even when it isn't running. You want to show a step count for how far they've walked? Use a notification. You want to trigger an alert because it's their turn to play in a game? Use a notification. You want to send them marketing messages to make them buy more stuff? Actually, just don't do that, you bad person.
Weâve only scratched the surface of what notifications can do, but if youâd like to explore more advanced topics â such as attaching pictures or letting the user type responses rather than tapping buttons â see my book Advanced iOS: Volume One.
Weâll be coming back to notifications again in project 33, where CloudKit is used to create and deliver remote notifications when server data has changed.
Review what you learned
Anyone can sit through a tutorial, but it takes actual work to remember what was taught. Itâs my job to make sure you take as much from these tutorials as possible, so Iâve prepared a short review to help you check your learning.
Challenge
One of the best ways to learn is to write your own code as often as possible, so here are three ways you should try your new knowledge to make sure you fully understand whatâs going on:
- Update the code in
didReceive
so that it shows different instances ofUIAlertController
depending on which action identifier was passed in. - For a harder challenge, add a second
UNNotificationAction
to thealarm
category of project 21. Give it the title âRemind me laterâ, and make it callscheduleLocal()
so that the same alert is shown in 24 hours. (For the purpose of these challenges, a time interval notification with 86400 seconds is good enough â thatâs roughly how many seconds there are in a day, excluding summer time changes and leap seconds.) - And for an even harder challenge, update project 2 so that it reminds players to come back and play every day. This means scheduling a week of notifications ahead of time, each of which launch the app. When the app is finally launched, make sure you call
removeAllPendingNotificationRequests()
to clear any un-shown alerts, then make new alerts for future days.