Configure Apple Push Notifications (APNs)
Photo Credit – 4585763335Johan – Apple Push Notifications

Configuring Apple Push Notifications (APN)

Here are the steps to configure Apple Push Notifications on your IOS app
1. Open project settings, Capabilities Tab

2. Turn On Push Notifications as shown below

How to turn on Apple Push Notifications (APN)


3. For security reasons Push Notifications require appropriate SSL Certificates which can be created through your Developer Portal. Here are the steps to create the required certificates.

3 A. Login to your Apple Developer portal

3 B. Click on App IDs under Identifiers

3 C. Click on your IOS App ID entry which shows the services enabled for your app

3 D. Click the Edit button at the bottom of the screen

3 E. Turn on Push Notifications (if not already enabled)

3 F. Click the Create Certificate button under the Development SSL Certificate 

3 G. Open Keychain application and Request a Certificate from a Certificate Authority as shown below

Request a certificate from a certificate authority

3 H. On the Certificate information add your email address, Name (make sure there are no spaces) and Save to Disk

3 I. Double click the certificate to add it to your Keychain.

3 J. Now find the certificate in your Keychain and expand the arrow next to it which will also show the Private Key associated with it

3 K. Using Shift button select both i.e. the Certificate and the Key and Export both the items in .p12 format. Name the file as certificatesDev.p12

3 L. You may choose to add your own password or leave it blank. If you add a password make a note of it somewhere.

3 M. Using below command in Terminal covert the .p12 format to .pem

4. Add below functions to your project’s AppDelegate.swift file

didRegisterForRemoteNotificationsWithDeviceToken gets the device token and passes the same along with some other information like systemversion, model etc to a php page which in turn stores the information in probably a MySQL database. This is the database of tokens to which we will be sending our Push Notifications.

5. Now add below code to the appDelegate.swift’s didFinishLaunchingWithOptions function

The above code basically calls the functions we created in Step 4

Apple Push Notification Payload

Push Notification Payload defines the format for the remote notification. Below is a basic format of a sample message.

For other fields refer to the Apple Documentation

At this point all the requirements to send and receive a notification are completed. However until this point when the user receives a notification, the message would just open the app and would do nothing else.

In most cases we would want the user to open a notification and perform certain task/s. Here are some examples.

  1. Download latest news items and open the View Controller associated with it
  2. Show a detailed message from the web related to an event, etc (Inside a Webview)

To achieve the above, we need something to handle the remote notifications inside your app.

This part mostly depends on what you want to achieve. Here we will just see an example. Let’s assume we want to promote an event and the notification should show the events page from the web on WebView added to a View Controller.

In a nutshell when the user taps the notification, instead of showing the dashboard of your application you want to automatically make him navigate to a certain View Controller which downloads a web page and shows the content of it in a Web View.

So here are the steps

1. Open your project settings page and turn on Remote notifications on the capabilities tab

2. Now your payload would include a URL to the webpage

In the above payload, link is not a defined field. It is a custom field which we will be reading in our app.

3. Create a View Controller named as NotificationViewController in your app and add a web view to it which opens an external link. The View Controller should be able to accept the payload as NSDictionary. Give some ID to the controller probably “notification

4. Now add below function to AppDelegate.swift file

Above function gets triggered when the user taps on the notification received because we have enabled remote notifications. This function basically just gets the payload and sends it to the NotificationViewController in your app.

5. The NotificationViewController grabs the link part from the payload and just opens the respective page.

Sending Push Notifications

The only part now remains is the method to send push notifications

While your app is under development you can test sending notifications using APN Tester

Once you are happy with it, you can create your own system to send notifications payload

Sandbox Notifications link: ssl://

Production Notifications link: ssl://

Remember to include the respective .pem file to send the notification payload


Leave a Reply

Your email address will not be published. Required fields are marked *