Home iOS & Swift Books Push Notifications by Tutorials

5
Apple Push Notification Servers Written by Scott Grosch

In the last chapter, you set up your app to be able to receive push notifications. The last piece that you’ll need in order to have your app receive a push notification is an Authentication Token used by Apple’s servers to trust your app. This token validates your server and makes sure that there’s always a secure connection between your backend and APNs.

Token types

When Apple first started allowing sending push notifications, it used the PKCS #12 archive file format, also commonly known as the PFX format.

If you’ve ever worked with push notifications in the past, this file ends with the .p12 file extension.

This type of format was quite cumbersome to work with for multiple reasons:

  • They are only valid for a single year, requiring yearly “maintenance” of your certificates.
  • You need separate certificates for both production and development distributions.
  • You need separate certificates for every app you publish.
  • Apple does not provide the certificate in the “final” format you’ll actually need to send notifications, requiring you to run multiple openssl commands from Terminal for the multiple conversions; usually requiring a bit of research to remember how.

Around 2016, in order to work around the above problems, Apple started supporting the industry standard RFC 7519, better known as JSON Web Tokens, or JWT (https://jwt.io/). These tokens use the newer .p8 file extension.

Apple, of course, likes its own names and so all of its documentation on push notifications refers to these as Authentication Tokens. Changing to the newer format alleviated all the issues of the PFX file format as they do not need to be renewed, don’t differentiate between production and development, and can be used by all of your apps.

Unfortunately, Apple did very little other than say, “There it is!”, when it released it. Unless you already had experience with HTTP/2 and JWT, you were stuck. We’ll remedy that now!

Getting your Authentication Token

Creating the Authentication Token is a simple process that you only have to do once. In a browser of your choice, go to the Member Center (https://apple.co/2HRPzxv), and sign in with your Apple ID.

  1. In the side-bar, under the Keys section, click on All.
  2. In the upper-right corner, click on the + button to create a new key.
  3. Name the key Push Notification Key.
  4. Under Key Services, select the checkbox next to APNs.
  5. Press Continue.

After these steps, simply confirm your key creation and download the file. By default, it will go to your Downloads directory and will be named something like AuthKey_689R3WVN5F.p8. The 689R3WVN5F part is your Key ID, which you’ll need when you’re ready to send a push.

You’ll also need to know your Team ID, so grab that now. Still in the Member Center, at the very top-right, click on Account. On the page that loads, in the sidebar, click on Membership. Your Team ID will be displayed on the details page.

The steps just described are illustrated, here:

Sending a push

At this point, you have everything you need to send a push notification to your app. You’ll need some way to actually configure the push and send it manually. There are many free, open-source, projects on GitHub to provide this functionality; consider using PushNotifications (https://bit.ly/2jvEUtK) as it supports the newer Authentication Keys, which some of the other apps don’t. You can use any of the apps out there as long as they support Authentication Keys.

Note: The simulator does not support push notifications.

Build and run the app from Chapter 4, “Xcode Project Setup,” in Xcode, being sure to run against a physical device as the simulator does not support push notifications. Once the app has successfully launched, move the app to background, by either switching to your home screen or locking the device. With the way your notifications are configured right now, you’ll only see them if you are not currently running the app in the foreground. You’ll fix that later on in the book, in Chapter 8, “Handling Common Scenarios.”

In your Xcode console window ( + + C), you’ll see a long hex string printed out, which is the token from your print call during registration. Copy that string into your clipboard.

Now, launch the PushNotifications app you downloaded from GitHub (or any other similar tool). You’ll need to be sure to select the TOKEN authentication option and then select the p8 file you downloaded from the Developer Portal, and fill in your Key ID, Team ID, Bundle ID and Device Token.

You don’t need to change the payload at all. Once you press Send, you should see a notification appear on your device shortly thereafter!

Key points

  • For your app to receive push notifications, you must have an Authentication Token used by Apple’s servers to trust your app.
  • The authentication token validates your server and makes sure that there’s always a secure connection between your backend and APNs.
  • Creating the Authentication Token is a simple process that you’re only required to do once; follow the steps in the chapter to create yours.
  • To configure the push and send it manually, there are many free, open-source, projects on GitHub to provide this functionality — just make sure whatever you use supports Authentication Keys.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.

Have feedback to share about the online reading experience? If you have feedback about the UI, UX, highlighting, or other features of our online readers, you can send them to the design team with the form below:

© 2021 Razeware LLC