How to Send Push Notifications to iOS Devices Via .p8 Key using Amazon Pinpoint

Amos
6 min readAug 6, 2020

--

Image courtesy of Apple Inc

In 2015, Apple released HTTP/2 API a new and simplified authentication process against the Apple Push Notification service.

I am going to refer to Apple Push Notification Service Notifications as APNs

Token-based authentication offers a stateless way to communicate with APNs. Stateless communication is faster than certificate-based communication because it does not require APNs to look up the certificate, or other information, related to your provider server

Establishing a Token-Based Connection with APNs

To start using token authentication, you first request a signing key(.p8 key) from the Apple developer account. Your server will then establish a connection without a client certificate and before sending notifications on this connection, your server will then construct an authentication token that includes your team ID and then sign it using the private key.

Images courtesy of Apple Inc.

Benefit of .p8 keys

  1. This .p8 key does not expire, so there is no need to worry about expiration date.
  2. Notification payloads can now be up to 4 KB
  3. This .p8 key can be used for both development and production environment, so there is no need to generate two separate certificate for each mode.
  4. Token based authentication is faster than certificate-based communication because it does not require APNs to look up the certificate or other information, related to your provider server.
  5. Synchronous feedback. This feature provides detailed error descriptions and information on unsubscribed devices over the same connection without closing it

Getting Started

The first thing we need to do is create an APNs Auth key (.p8 key)

Step 1: Generating .p8 key file

To generate a .p8 key file, go to Apple developer account page , then select Certificates, Identifiers & Profiles.

Next, select Keys from the Left Menu.

In the new key page, type in your key name and check the Apple Push Notification service (APNs) box , then click Continue.

Click Download to download your Auth key file. Copy and save the Key ID which you’ll need later. Note that you can only download the file once. Make sure you save this file properly and create a backup for later. Do not rename the file.

Important: Save a back up of your key in a secure place. It will not be presented again and cannot be retrieved later.

The Auth Key filename will look like this : AuthKeyABCD1234.p8, the ABCD1234 is the Key ID for this .p8 Auth key.

If you suspect that your authentication token signing key has been compromised, revoke it and request a new one. (You revoke the key from your developer account on developer.apple.com in the same place where you created it.).

Step 2: Backed-End (AWS Pinpoint) Configuration

Image courtesy of Amazon Web Services

Amazon Pinpoint, is a service that makes it easy to understand your users’ behavior, define who to target, what messages to send, when to deliver them, and tracks the results of the campaign

Now that we have the .p8 push notification key, let’s head to Pinpoint console to configure the push notification settings with this key file we’ve downloaded.

In your Pinpoint console, select your project :

If you do NOT have a project to select from, then you will need to click Create a project button to create a new project.

Click on settings (usually located on the left-hand side of the Pinpoint console) and choose Push Notifications

Click Edit and under Push notifications services choose Apple Push Notification Service (APNs) as shown below:

On the next screen under Push notifications services section, choose Apple Push Notification Service (APNs) as shown below:

To send push notifications to your iOS app, Amazon Pinpoint supports two authentication mechanism methods:

  1. Key credentials
  2. Certificate credentials
In this demo we’ll be using Key credentials

Click Key credentials to enter your authentication details

  • Team ID — Open your Apple Developer account and select membership section to retrieve the Team ID value..
  • Key ID — Open your Apple Developer account, select Key section and choose the Key that you have created earlier to get Key ID value. You can also get the Key ID from the .p8 key file you downloaded from Apple developer account. For example if your .p8 key is AuthKeyABCD1234.p8, then ABCD1234 is the Key ID..
  • Bundle ID — Open your Apple Developer account and select Identifiers section to retrieve the Bundle Identifier value
  • Authentication Key — Upload the .p8 key file you downloaded from Apple developer account

Step 3: Xcode Configuration

Create an iOS project, if you haven’t created one before. Make sure the Bundle Identifier you use matches what you’ve specified in Step 2 in Amazon Pinpoint push notification settings.

Augment AppDelegate.swift

Now, we need to add the code to integrate the Amazon Pinpoint iOS SDK and iOS Push Notification library into our AppDelegate file. The AWS Amplify docs for iOS does a good job in providing us with a sample code snippet.

The complete AppDelegate.swift would resemble something similar like the below:

Once we build and run our iOS app we should get sample device token in the Xcode terminal window as below :

copy this token somewhere, we’ll use it for testing

Step 4: Testing

Amazon Pinpoint console, provides two ways to send push notifications :

i) Campaigns

ii) Test messaging

If you’re not familiar with how to use Amazon Pinpoint’s Test messaging feature, check out the following gif :

Test-messaging feature

If everything is configured correctly, you should receive your first push notification:

A couple of things to note is that 1. For iOS push notification, you should always test this on a real device (the device you have registered in your Developer account) 2. If your app build was signed with production APNS entitlements, then select APNs mode from Pinpoint console when you send a notification or vice versa

I’m a Cloud Engineer/Developer and enjoy working with AWS services like Pinpoint and AWS Amplify framework.

Thank you for reading. If you encounter any trouble setting this up, let me know in the comments below.

If you like the post, hit the 👏 button below and share it!

--

--

Amos

Cloud Developer and Engineer. Interested in #mobile, #serverless, #cloud, #travel and #gaming