Chapters

Hide chapters

Push Notifications by Tutorials

Third Edition · iOS 14 · Swift 5.3 · Xcode 12

Before You Begin

Section 0: 4 chapters
Show chapters Hide chapters

Section I: Push Notifications by Tutorials

Section 1: 14 chapters
Show chapters Hide chapters

5. Sending Your First Push Notification
Written by Scott Grosch

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

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.

Authentication 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 (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 (apple.co/2HRPzxv), and sign in with your Apple ID.

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.

Push notifications on simulator

While you should always test your push notifications against a physical device, during day-to-day development it’ll be easier to test on the simulator so you don’t have to continuously unlock your device.

{
  "aps": {
    "alert": "Hello"
  },
  "Simulator Target Bundle": "com.raywenderlich.PushNotifications"
}

Push notifications on device

There are many free and open-source projects on GitHub which will allow you to send a push notification to your device; consider using PushNotifications (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.

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 and open-source projects on GitHub to provide this functionality — just make sure whatever you use supports Authentication Keys.
  • If you’re sending a push notification to the simulator, ensure the file has the apns extension and includes your bundle ID.
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.
© 2024 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now