Change settings and code the AppDelegate to tell the system to use a remote notification.
- [Instructor] In order to use push notifications, you have to tell the system you want push notifications before you do anything else. For this reason, you'll be coding push notifications in the app delegate. Open up the appdelegate.swift file. And under the import UI kit, add the notifications framework. Like any other notification, you must ask for permission before using them. For this app, I'll ask for permission for the alert sound and badge. Before return true in the application did finish launching with options launch options method.
Put a couple spaces. Add the UNUserNotifcationCenter.current and the the request authorization. In the options, we'll use all three: .alert comma, .sound, and .badge. Double-click on the completion handler, change the bopl to granted. And error to Error? to error.
In the code, if granted, and then we get to something that's a little bit new. Instead of just setting granted as we did before on local notifications, we're going to go to application, register for remote notifications, and register our notifications. Else, print the error.
User notification, permission denied, and it's an error. Localize description. If you've made a local notification before, most of this code should look familiar. You request authorization for an alert, sound and badge notification. In the closure, you deal with the results of that request. If not granted, you post a message with an error to the console. You can change this code as you wish, possibly adding an alert if the user refuses.
Unlike local notifications, if you are granted access the application, register for remote notifications method registers the app to receive notifications from APNS. There's two more methods in the app delegate you'll have to add. Both of which you should find in the auto-completion of xcode. Just under here, add the first one, which is application did register for remote notifications with device token. Here, just put a comment for the moment, we're going to come back to it.
This method runs if registration is successful. You'll send the device token in the argument from here to your provider. We'll get back to that later, so for now, just add a comment. The second method, applications did fail to register for remote notification with error, runs if the registration is not successful. That could be for lack of internet connection, lack of certificate, or you are running on the simulator which prohibits remote notifications, among other reasons.
Add the application did fail to register for remote notifications with error, to print an error to the console. So we're just going to put a little print here. You've setup the code to register the notification, since push notifications deal with outside your app, your next step is to tell Apple you're talking with the outside world.
- Reviewing local notifications, APNs, and push notifications
- Starting a push notification project
- Registering for a notification
- Getting a certificate
- Sending a notification
- Working with JSON payload push notifications
- Adding a notification service extension
- Altering the UI
- Coding the view controller
- Running the notification