Register for a notification

- [Instructor] In order to use push notifications,you have to tell the system you want push notificationsbefore you do anything else.For this reason, you'll be coding push notificationsin 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 askfor permission before using them.For this app, I'll ask for permissionfor the alert sound and badge.Before return true in the application did finishlaunching with options launch options method.

Put a couple spaces.Add the UNUserNotifcationCenter.currentand 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 getto something that's a little bit new.Instead of just setting granted as we did beforeon 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, soundand badge notification.In the closure, you deal with the results of that request.If not granted, you post a message with an errorto the console.You can change this code as you wish, possibly addingan alert if the user refuses.

Unlike local notifications, if you are granted accessthe application, register for remote notificationsmethod registers the app to receive notifications from APNS.There's two more methods in the app delegateyou'll have to add.Both of which you should find in the auto-completionof xcode.Just under here, add the first one, which isapplication did register for remote notificationswith 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 argumentfrom 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 remotenotification with error,runs if the registration is not successful.That could be for lack of internet connection,lack of certificate, or you are runningon the simulator which prohibits remote notifications,among other reasons.

Add the application did fail to register for remotenotifications 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 Appleyou're talking with the outside world.

Resume Transcript Auto-Scroll

Author

Released

2/23/2017

Using the power of external servers, application push notifications provide helpful information like flight change information, sports scores, and news stories—even when you are not using the app. In this course, Steven Lipton covers push notifications in iOS 10, and shows how to set up an app to accept push notifications and which tokens and certificates help make your notifications work. Plus, he explains how notification payloads work, and what service and content extensions mean for customizing your app's notifications.