mPower™ Blog Series

Part 2: How to setup Push Notifications in iOS

Introduction

In the first part of the blog series on push notifications, you have been provided an overview of what push notifications are all about, basic pre-requisites for implementing push notifications in iOS, key takeaways and some of challenges pertaining to push notifications. This blog is a continuation of the earlier blog which focusses on the configuration aspects of push notifications.

As outlined in the first part of this blog series, Apple introduced push notifications to let applications respond to events, even if the applications aren’t running in the forefront. Push notifications notify an application about vital events and to enable the engagement of the users.

Pre-requisites

Before we start configuring push notifications, let’s recall some of the basic pre-requisites, already covered in my earlier blog.

Put it in a simplest manner, you need two things to get started with the process of sending push notifications:

  1. Requirement of a physical device (since iOS simulator doesn’t support push notifications)
  2. Requirement of a paid iOS developer account; only paid accounts can provision applications to run on a physical device.

I. Project Setup

  1. Click Single View Application template to open Xcode to create a new project, based on the single view application template.

Picture - 1

2.  In the Choose options for your new project: screen:

  • Type push in the Product Name box.
  • Type a company identifier and class prefix in the respective boxes.
  • Click iPhone from the Devices box.

Picture - 2

II. Registration

  1. Open TSPAppDelegate.m and update the application:didFinishLaunchingWithOptions: as shown below.

Here, you are calling registerForRemoteNotificationTypes: on the application object, passing in the notification types you are                                  interested. This ensures that the operating system now knows that the application is configured to receive push notifications.

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
               // Register for Remote Notifications
               [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |                                          UIRemoteNotificationTypeSound)];

               return YES;
               }

The operating system contacts the Apple’s servers and gets a “device token” to uniquely identify the device from which the application is running. This device token is used by your server infrastructure to send push notifications. This is accomplished by sending the device token along with the actual push notification to Apple’s servers. Apple servers take the charge of distributing the push notifications to the appropriate devices.

Make a note that the device token differs for each application and it can even alter over time for the same application. Apple therefore recommends asking for a device token every time the application is launched and send the device token to your backend to ensure that the device token is up to date.

The following methods tells your application whether the registration for remote notifications is successful or not:

  •  application:didRegisterForRemoteNotificationsWithDeviceToken:
  • application:didFailToRegisterForRemoteNotificationsWithError

As of now, implement these methods as shown below.

(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@”Did Register for Remote Notifications with Device Token (%@)”, deviceToken);
}

(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@”Did Fail to Register for Remote Notifications”);
NSLog(@”%@, %@”, error, error.localizedDescription);

Both methods are declared by the UIApplicationDelegate protocol. This protocol also declares another method, application:didReceiveRemoteNotification:, which is invoked when the application receives a remote notification.

The application:didReceiveRemoteNotification: method delivers you the payload of the push notification as an NSDictionary object. Your application needs to decide how it should respond to the push notification.

If you run your application, then the application:didFailToRegisterForRemoteNotificationsWithError: method will be invoked.

III. Configure SSL Certificate creation

To complete the next step, you need to sign into your iOS developer account at Apple’s iOS Dev Center.

The following are the steps to accomplish this:

  1. In the Certificates, Identifiers and Profiles screen, click Identifiers under the iOS Apps section.

Picture - 3

2.  Click the + button on the top right corner and type an App ID description to help you identify the App ID later.

Picture - 4

3.  Click Explicit App ID, instead of Wildcard App ID from the App ID Suffix box.

4.  If you want the application to receive remote notifications, type com.tutsplus.push (instead of com.tutsplus) in the Bundle ID box.

Picture - 5

5.  Under App Services, click Push Notifications. Click Continue to submit the form and finally click Submit to create the App ID.

Picture - 6

Picture - 7

 

6.  From the list of App IDs, select the one you just created and click Edit.

7.  Scroll down until you see the section that covers push notifications, where you can view two buttons labeled Create Certificate as shown below:

Picture - 8 IV. Create an SSL certificate

  1. Select Keychain Access on your development machine.
  2. From the Keychain Access menu, click Certificate Assistant > Request a Certificate From a Certificate Authority. Double-check to ensure that no key is selected in Keychain Access, when you select this option.
  3. In the Certificate Assistant screen, type the email address and a common name to help you identify the certificate later and click Saved to disk.
  4.  Click Continue and save the certificate signing request to your hard drive. This way you’ve created a certificate signing request as well as public and private keys. You can view the keys in the Keychain Access (as shown in the screen shot below).

 

Picture - 9

 

Picture - 10

 

Picture - 11

5.  Revert to the iOS Dev Center and click Create Certificate.

6.  Next click Continue, click Choose File to upload the certificate signing request and click Generate to generate the SSL certificate.

Picture - 12

7.  Now download the certificate and double click on it to install it in Keychain Access.

8.  Double-check that the certificate is added to Keychain Access and linked to the appropriate private key.

Picture - 13

V. Create a provisioning profile

Before you proceed to test your push notifications setup, you need to create a provisioning profile for your application.

  1. From the iOS Dev center, click Development in the Provisioning Profiles section.
  2. Click the + button on the top right corner and click iOS App Development under the Development section.

Picture - 14

3.  Click Continue and select your App ID from the list

4.  Select the certificates you want to include in the provisioning profile and click Continue.     

              Note: Make sure your test device is also included.

5.  Type an appropriate name for your provisioning profile and then click Generate.

Picture - 15

6.  Download the provisioning profile and drag it in Xcode to add it.

7.   Update your target’s building settings in Xcode to use the new provisioning profile.
8.   Build and run your application to ensure that everything is proper and works accordingly, as desired.

If you encounter any issues while running your application, double-check to ensure that the bundle identifier of your application                   matches with the App ID (bundle identifier is case sensitive).

If you have followed all the steps as outlined above, your application should generate a prompt message as follows:

Picture - 16

9.  If you tap OK, your application will ask the operating system for a device token. In case if it is successful, the application:
didRegisterForRemoteNotificationsWithDeviceToken: method of the UIApplicationDelegate protocol is invoked, handing you the device token. Since we have already included a log statement to this method, the device token should also be logged to the console in Xcode.
Push[2182:60b] Did Register for Remote Notifications with Device Token ()
}

VI. Ensure proper backend is in place

Now that you have successfully accomplished all the above steps, the final step is to make sure that you have a backend in place to ensure that your application can send the device token to it. This is required to test whether your push notifications you had send have successfully arrived or not. That backend can then connect with Apple’s servers to send push notifications. This is the simplest part of the configuration, especially when you are using a service like Parse or Urban Airship.

Conclusion

I hope this blog has provided you an overview of the configuration setup for successfully sending push notifications from your device. This configuration setup is not at all difficult as some developers perceive, though there are certain challenges involved in understanding the concepts of keys and properly generating the certificates.

Request a Demo

If you would like to request a demo of Innovapptive’s Native, HTML5 or Hybrid apps, please click on the link. Alternatively, if you would like to discuss with an Innovapptive solution expert, you can reach out to us by emailing us at sales@innovapptive.com or you can reach a sales representative at (713) 275-1804.

Share this post

Leave a comment

Your email address will not be published.