Push Notifications for iOS : Updated iOS 10+

What are Push Notifications?

Push notifications is a kind of message which an App publisher wants to notify a user. It is a server pushed message.

For Example, there is a mobile application which belongs to News domain. An application of such a domain needs to update its user with the happenings in the world as earliest as possible to stand in the market. and their user also wants the same, the application should keep them up to date about the events happened in the world. So, any kind of breaking news or latest updates of the market can be notified to the user with the help of Push Notifications.

How to implement Push Notifications?

Please follow the below steps for implementing Push Notification for an iOS Application.

  • Create a Simple XCode Application.
iOS Tutorials: Create a Single View App.
Create a Single View App.

 

  • Give a name to the Application, and choose the desired team and identifiers.
iOS Tutorials: Give App Name, choose Team and Identifiers.
Give App Name, choose Team and Identifiers.
  • Save the project to the desired location.

 

iOS Tutorials: Save Project to desired location.
Save Project to the desired location.
  • Once the Application is created, navigate to the capabilities section.

 

iOS Tutorials: Navigate to Capabilities Section.
Navigate to Capabilities Section.
  • Toggle the Push Notification switch to “ON“, there will be a small window asking to select the, please select the desired team from the drop-down.

 

Toggle Push Notification Settings
Toggle Push Notification Settings

Note: Before getting into coding we need to make sure we have valid App Certificates, Push Notification Certificates, Valid Provisioning Profile. a .pem file is also required in case of a PHP based back-end server. A .p12 certificate would do for Firebase Cloud Messaging and Microsoft Azure-based Push Notifications.

Please visit the link and follow steps for creating valid App Certificates, Push Notification Certificates, Valid Provisioning Profile, and .pem file.

  • Navigate to AppDelegate.swift file.
  • Add the below snippet of code in the file.

 

Code Snippet in AppDelegate File
Code Snippet in AppDelegate File
  • Call the method “registerForPushNotifications()“, in the didFinishLaunchingWithOptions method.

 

iOS Tutorials: Call regsiterForPushNotifications Method
Call regsiterForPushNotifications Method in didFinishWithLaunchingOptions.
  • Please select a valid team and Provisioning Profile.

 

iOS Tutorials : Please Select Valid Team and Provisioning Profile
Please Select Valid Team and Provisioning Profile
  • Please select the desired device (Physical device) and run the application (Command + R).

 

iOS Tutorials: Please select a physical device.
Please select a physical device.

Note: Here I am using Mac’s default application QuickTime Player so that we can show you how the device is behaving. After we run the application, there will be an alert asking for permission to Register for Push Notifications. Please click on “Allow“.

iOS Tutorials: Click on Allow
Click on “Allow” to receive a Valid device Token
  • A Device Token will be received. This Device Token is the key which will be used for sending Push Notifications to the user. Keep this Device Token For further use.

 

iOS Tutorials: Received Device Token
Received Device Token.

Note: Here we will be using APN Tester App on Mac to test Push Notifications, the Application can be downloaded from the link here.

This Application uses the .cer file which can be downloaded from the Apple Developer Console and the device token which was generated in the XCode Console.

  • Download the .cer file from the apple developer console.

 

iOS Tutorials: Download .cer file
Download .cer file from Apple Developer Console.
  • Install the APN Tester App and open the application. Copt paster the Device Token, Browse the .cer file and enter the below Payload.
{
"aps": {
"alert": "Hello From APNs Tester.",
"badge":"1"
}
}

 

iOs Tutorials: Enter Required details in the APN tester App to received Push Notifications.
Enter Required details in the APN tester App to receive Push Notifications.

Note: Make sure the application is in the background.

Author: Chandra.rao

iOS Developer. #Life of an developer.... Learn New technology, Code, Learn New Technology, Code, Death.