Telerik blogs

Push notifications are useful for keeping users informed with timely and relevant contents. Push notifications can be sent regardless whether the app is running, inactive or in the background. Notifications can display an alert; play a distinctive sound or update badge on your app icon.

In this post, I will walk through the process of enabling push notification for an app in Apple developer portal, upload the generated certificate to Everlive and finally register my device in Everlive to receive push notifications.

Before you begin, note that you cannot test push notifications in iOS simulator. You need an iOS device as well as an Apple developer account. You also need to sign up for Everlive in order to send push notifications using its service.

Create the SSL certificate

At this point you will be creating the SSL certificate that is to be associated with the IOS app in Apple developer portal.

You will first launch the Keychain Access application in your mac.  Next go to Menu-> Keychain Access -> Certificate Assistant - > Request a Certificate From a Certificate Authority

This will bring up the following wizard:

Here you will select “Saved to disk” option and once completed it will prompt you to save the .certSigningRequest file in your mac.

Create the App ID

In this step you will create the App ID in Apple Developer portal. App ID is basically the unique identifier of the iOS app generally represented by a reversed address (ex. com.telerik.EverliveApp). Here to note that this identifier should not contain any wildcard (*) characters.

Steps for creating an App ID are listed below:

  1. Navigate to “Apple Developer Member Center” and select  “Certificates, Identifiers & Profilers”
  2. Select “Identifiers” from the menu on the left.
  3. Select Identifiers -> App ID then on top right then click the “+” button to create a new one.
  4. Complete the form with “App ID Description” and “Explicit App ID” which is the bundle identifier of your app that is located in info.plist file of your app.
  5. On same page, select the services that you want to enable. In this case, I have selected “Push Notifications”.
  6. Press, “Continue” to save.

Configure the App ID

Configuring the App ID includes creating the development certificate to receive push notifications.

  1. Select “identifiers” from previous step and select the App ID that is newly created.This will bring the following screen:
  2. Click “Edit” and scroll down to Push Notifications section.         
  3. Press “Create Certificate” under "Development SSL Certificate" that will bring up a wizard to upload the .certSigningRequest file that you saved in your mac earlier.
  4. Download and install the generated SSL certificate into your keychain.
  5. In Keychain Access it will be installed under “My Certificates”. The certificate should be called “Apple Development iOS Push Services”. Right-click on it and select “Apple Development iOS Push Services ...” and export it as a .p12 file.     

Create Provisioning profile

Once the App ID is created and configured, the next step is to create a provisioning profile based on it in order to deploy the app in a device.

Steps for configuring a provisioning profile are:Go to “Certificates, Identities and Profiles” from “Apple Developer Member Center”.

  1. Go to “Certificates, Identities and Profiles” from “Apple Developer Member Center”.
  2. Select Provisioning Profiles -> Development and click the  “+” icon on top right corner. This will take you to create new wizard.      
  3. Select Development -> iOS App Development and choose the App ID created in previous step from the dropdown.
  4. Select the development certificate that is associated with the developer account (ex. iOS Development)
  5. Select the device that is previously added to the portal (Using Xcode).
  6. Name the provisioning profile and click “Generate”. In Xcode 5 you can download this profile from preferences menu or in earlier version you can do the same using organizer.

Configure Everlive

Now the .p12 file that is created in previous step should be uploaded to Everlive portal. This will be used by Everlive to send notifications to a registered device through Apple Push Notification Service (APNS).

In order to do so select the app which you want to configure. Go to Settings-> Push Notifications and upload your .p12 file under iOS-> Development.

Register the device

In order to receive notifications in an iOS device, you have to call [application registerForRemoteNotificationTypes:] method in appDelegate’s [application didFinishedLaunchingWithOptions:] method. This will prompt the user (first time the application is run) if he wants to allow push notifications for the app and once he confirms,
the following method will be called:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken{
   [Everlive setDeviceToken:newDeviceToken];
}

Here I am storing the device token in client library singleton that will be used later to register the device in Everlive.

As the registerForRemoteNotificationTypes is called, it will validate the App ID and profile that is used to deploy the app. In case of any errors, it will call the following method in app delegate:

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"Registration:%@", error); abort();
}

Device is registered in Everlive by calling the following method from the SDK:

[[Everlive sharedInstance]registerDevice:^(BOOL success, NSError *error) {
    // nothing here for now.
}];

If everything went right, sending a notification from Everlive cloud will call the following method in app delegate:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
 [Everlive handlePush:userInfo];

}

When a push notification is received while the app is not in foreground, it is handled by iOS notification center. However, when the app is active it is developer’s job to handle the notification in the method mentioned above.  Here, I just called the [Everlive handlePush:userInfo] method that creates an UIAlertView dialog to show the notification.

It is also possible to unregister a device from Everlive.  You can do this in the following way:

[[Everlive sharedInstance] removeDevice:^(BOOL success, NSError *error) {
     // completed;
}];

Here to note that [Everlive sharedInstance] contains your default API key that you initialized either in [application didFinishedLaunchingWithOptions:] or info.plist file. Please refer to the getting started with Everlive iOS SDK section for more.

In addition, it is also possible to set custom parameters while registering the device. This will be useful in filtering a range of devices for sending push notifications.

Parameters can be set in the following way:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Friends",@"appName", nil];
[[Everlive sharedInstance] registerDeviceWithParameters:dictionary block:^(BOOL success, NSError *error) {
   // TODO:
}];

Send Push Notifications

Before you deploy the app in a device and start receiving push notifications. You have to the set the provisioning profile containing the App ID created previously in Build Settings -> Code Signing -> Provisioning Profile property.

Once done, you can send notifications to your device using the following command:

curl -X POST -H "Content-Type:application/json" \
--data '{"Message": "Sample Push Notification"}'\ http://api.everlive.com/v1/ApiKey/Push/Notifications

However, it is also possible to send notifications from Everlive admin portal. Navigate to App -> Push Notifications -> Add New Push Notification where you can further filter devices based on custom parameters:

Summary

In this post I have created a provisioning profile from an App ID with push notification enabled. I then walked through the process of registering the device in Everlive and sent a notification using curl.  Everlive and equivalent providers simplify the process of sending push notifications to your device. Apple Push Notification service transports and routes a notification from a given provider to a given device.

It is possible to write your own provider, however in that case you have to manually configure the payload dictionary, validate device and deal with security.  Moreover, each platform handles push notifications differently like Android uses Google cloud. In that case, you have to manually implement providers for each platform. Everlive on the other hand will take care of the platform specific details and relay your notifications to all associated devices without requiring you to go under the hood.

Finally, I have skipped the configuration of Everlive SDK in Xcode, which is beyond the scope of this article. Please refer to the original SDK documentation @http://docs.everlive.com to dig further.


Comments

Comments are disabled in preview mode.