Blog

iOS Push Notifications with Backendless

by on July 31, 2013

In this blog post we review the process of setting up and developing an iOS application capable to receive Apple Push Notifications. We also review the functionality of publishing a push notification through Backendless.

Creating App ID

  1. First we are going to create an App ID for the application which will receive Push Notifications. Login to Apple Developer Member Center. Click on “App IDs” in the “Identifiers” section. Use the plus sign “+” button to create a new ID:
  2. When prompted enter App ID Prefix. Make sure it is descriptive enough so you recognize it later when you return to the Member Center.
  3. Select Explicit App ID in the “App ID Suffix” section and enter the same bundle ID which you will be using in the application:
  4. In App Services select the services which the application will use and click “continue”:
  5. Make sure that Push Notifications are enabled and click “submit”. This will conclude the App ID creation for the app:

Creating Certificate Request

Push Notifications require a certificate which will be used on a device by the means of a provisioning profile. Also the same certificate (transformed to the Personal Information Exchange – .p12 format) will be used by Backendless to publish Push Notifications. If this makes little sense, do not worry, you will need to perform these steps only ones and then can move on to code and using the APIs.

  1. In order to create a certificate a Certificate Signing Request (CSR) must be issued. To create a CSR, open Keychain Access and select Keychain Access >> Certificate Assistant >> Request a Certificate from the main menu:
  2. Enter your email address and Common Name (leave the CA Email Address field empty), select “Saved to disk” and click “Continue”:
  3. Select a directory where to save the file and click Save.

Generating an SSL Certificate for Push Notifications

The CSR file created in the section above will be used to create an SSL Certificate. That certificate will then be used by Backendless to publish push notifications.

  1. Return to Apple Developer Member Center and select “All” under “Certificates”. Click the plus button “+” to add a new certificate:
  2. Select certificate type – there are two options Development and Production. For now select “Apple Push Notification service SSL (Sandbox)”:
  3. Select the App ID created earlier in these instructions:
  4. Next you will see the instructions for generating a CSR which you have already created by now. Click Continue to proceed to the next step.
  5. Select the CSR file created and saved to the disk earlier and click Generate:
  6. The certificate is ready now, click “Download” to download it:
  7. Add the certificate file to Keychain Access.
  8. Open Keychain Access and locate the certificate in the “My Certificates” section:
  9. Right click on the certificate and select the Export option:
  10. Save the certificate in the p12 format:
  11. Enter a password for the certificate. Make sure to make a record of the password – you will need to use it later in the instructions when you submit the certificate to Backendless:
  12. Enter your Mac OS X account password to confirm the action. At this point, you have a certificate for Push Notifications.

Configuring Backendless App with the Certificate

Since Backendless provides the actual server-side integration for delivering Push Notifications for your application, it needs to have access to the certificate you created above. The steps below provide the instructions for uploading the certificate into Backendless:

  1. Login to Backendless Console at http://develop.backendless.com/ and create/select an application which you will use on the server-side:

  2. Click Manage > App Settings. Locate the Mobile Settings section and upload the .p12 certificate created earlier. Make sure to enter the same password you used when created the certificate:

  3. Now your Backendless server is ready to publish Push Notifications.

Creating Provisioning Profile

  1. Login to Apple Developer Member Center and select “All” under Provisioning Profiles. Click the plus button “+” to create a new profile:
  2. Select the “iOS App Development” profile type:
  3. Click Continue. On the next screen select the App ID which was created earlier:
  4.  Click Continue. Select the users/certificates which will be included into the profile.
  5. Click Continue. Select the devices to include into the profile.
  6. Click Continue. Enter a name to assign to the profile and click Generate:
  7. Download the profile:
  8. Start XCode and open Organizer. Select the Provisioning Profiles section and add the profile you generated/downloaded:

Creating App in XCode

Finally, all the setup is done and we can proceed to the fun part – creating an app in XCode and writing some code.

  1. Open XCode and create new “Single View Application”.
  2. Enter “pushNotificationSample” as the Product Name. Also, provide your organization name and company identifier. Since the bundle ID we used when creating App ID earlier was “com.backendless.pushNotificationSample”, you should use “com.backendless” as the “Company Identifier”:
  3. Once the application is created, open Targets > Build Settings > Code Signing > Code Signing Identity and select the provisioning profile created earlier:



  4. At this point, you should add Backendless SDK to your project. This includes adding the Backendless library and its dependencies. Additionally, you will need to copy/paste Backendless Application ID and Secret Key into the code. For detailed instructions on how to do that, please follow the Backendless Quick Start Guide for iOS.
  5. Open AppDeletage.m. The following code registers the device with Backendless and Apple Push Notification Service:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      [backendless initApp:APP_ID secret:SECRET_KEY version:VERSION_NUM];
      NSDictionary *remoteDict = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
      if (remoteDict)
          [self application:application didReceiveRemoteNotification:remoteDict];
      [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
       UIRemoteNotificationTypeAlert
       | UIRemoteNotificationTypeBadge
       | UIRemoteNotificationTypeSound];
      return YES;
    }

    If the registration goes through, the following method is automatically called with the device token passed as the argument:

    - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
    {
      NSString *deviceTokenStr = [backendless.messagingService deviceTokenAsString:deviceToken];
      @try {
          NSString *deviceRegistrationId = [backendless.messagingService registerDeviceToken:deviceTokenStr];
          NSLog(@"deviceToken = %@, deviceRegistrationId = %@", deviceTokenStr, deviceRegistrationId);
      }
      @catch (Fault *fault) {
          NSLog(@"deviceToken = %@, FAULT = %@ <%@>", deviceTokenStr, fault.message, fault.detail);
      }
    }

    Otherwise, if the registration fails, the following method is called with the argument of the underlying error:

    - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
    {
       NSLog(@"APN Registration Error: %@", err);
    }

    In case of the successful registration, the device is ready to accept published Push Notifications. When a notification is delivered to a device, the following method is invoked when the app is running.

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    {
       NSLog(@"%@", userInfo);
       [(StartViewController *)[[(UINavigationController *)[self.window rootViewController] viewControllers] objectAtIndex:0] showNotification:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]];
    }
  6. In order to send a Push Notification from a device, use the following code:
    - (void)sendMessage
    {
       PublishOptions *publishOptions = [PublishOptions new];
       NSDictionary *headers = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"ios-badge", @"Sound12.aif",@"ios-sound", nil];
       publishOptions.headers = headers;
       @try {
           [backendless.messagingService publish:@"default" message:@"some alert message" publishOptions:publishOptions deliveryOptions:[DeliveryOptions deliveryOptionsForNotification:PUSHONLY]];
       }
       @catch (Fault *fault) {
           NSLog(@"sendMessage: fault = %@", fault.detail);
       }
    }

Publishing Push Notifications from Backendless Console

  1. Open Backendless Console at http://develop.backendless.com/ and select the Messaging section.
  2. Enter the text of the notification message in the “Message text” area, also make sure to specify the headers:
    1. “ios-badge”:”X”, where X is the number for the badge update
    2. “ios-alert”:”alert message text”
    3. “ios-sound”:”URL of the sound file” For a list of all supported headers, see the Publish Push Notification section in the documentation.
  3. Click the “iOS Devices” checkbox.
  4. Click Publish. The Push Notification will be delivered to all registered iOS devices:
  5. To see a list of the registered devices use the Devices tab. You can also send a push notification to specific devices. In order to do that, select the devices using the checkboxes and then click the “Selected devices” option under “Push Notification” section:

Leave a Reply