Community

Creating an iOS HelloWorld Application for Bluemix Mobile Services and Integrating IBM Push Notifications

Share this post:

Overview

The goal of this tutorial is to obtain and configure a simple iOS application utilizing the Bluemix Mobile Services. It will cover creating a MobileFirst Services Starter application using a boilerplate and configuring a HelloWorld application to connect to it. The tutorial will then go into extending the sample to include, configure and test the IBM Push Notifications service. The final goal is to have an iOS application that is capable of registering for and receiving push notifications:

Screen Shot 2015-10-22 at 4.06.37 PM

This will be completed through the following topics:

If a HelloWorld iOS application has already been obtained and configured using the Mobile Services, it is recommended to start at the Configure IBM Push Notifications on Bluemix section.

Create a MobileFirst Services Application

In order to utilize the Mobile features provided on Bluemix, a backend application must be created to connect the client successfully. To simplify the process a MobileFirst Services Boilerplate is available on Bluemix which includes the following services:

  • SDK for Node.js
  • Cloudant NoSQL DB
  • IBM Push Notifications
  • Mobile Client Access

To create the MobileFirst Services Application

a) Login to Bluemix.
b) In the CATALOG tab click the MobileFirst Services Starter tile under the Boilerplates section:

BoilerPlate

c) Enter a Name for the Application and click CREATE:

CreateApp

Please Note: The application has to be a unique name that has not already been created in the environment.

d) Once the application has been created successfully click the Overview tab. Please make note of the Route and App GUID of the newly created application. This can be found by clicking the MOBILE OPTIONS button on the dashboard:

MobileOptions

These values will be required in order to configure the client side application to this backend. Please make note of these values for future use.

Obtain, Configure and Build the HelloWorld Sample

This tutorial will utilize a HelloWorld sample in order to create the foundation for connecting a client to the Bluemix backend that was created. As a preliminary step the sample should be obtained, configured and tested in order to verify a valid connection with the backend Bluemix application.

Obtaining the HelloWorld Sample
The sample is currently hosted on Github in the following repository:
Bluemix Mobile Services – HelloWorld iOS

From a terminal window, download the sample using the following command:

git clone https://github.com/ibm-bluemix-mobile-services/bms-samples-ios-helloworld

GIT

Note: If Git is not installed it can be obtained at Git-Downloads.

Configuring the HelloWorld Sample
Once the sample has been downloaded from Git open the helloWorld.xcworkspace file. This workspace has all the Pods and dependencies pre-configured, while the .xcodeproj does not. The locations for each environment are listed below:

Objective-c: bms-samples-ios-helloworld/helloWorld_objective_c/helloWorld.xcworkspace
Swift: bms-samples-ios-helloworld/helloWorld_swift/helloWorld.xcworkspace

Once the sample is open in Xcode, initialization code must be added in the AppDelegate in order to test the connection to the backend Bluemix application we created previously.

Open the corresponding AppDelegate file and add the following code snippet inside the didFinishLaunchingWithOptions method. The ApplicationRoute and ApplicationGUID that were noted earlier should be used inside of these snippets:

Objective-C:

IMFClient *imfClient = [IMFClient sharedInstance];
[imfClient initializeWithBackendRoute:@"<insert route>" backendGUID:@"<insertGUID>"];

Swift:

IMFClient.sharedInstance().initializeWithBackendRoute("<insert route>", backendGUID: "<insertGUID>")

Running the Sample
In Xcode click Product->Run in order to run the application. It is recommended to run the application on a physical iOS device as it will be required when attempting to test with Push Notifications later in this tutorial.

To test the connection against the backend which has been configured inside of the AppDelegate, click the PING BLUEMIX button.

Screen Shot 2015-10-21 at 2.21.55 PM

If a valid connection is made a Yay! You are Connected message will be displayed:
Screen Shot 2015-10-21 at 2.25.50 PM

If a connection cannot be made a Bummer Something Went Wrong message will be displayed, followed by a small description of the error. To troubleshoot these issues please verify the ApplicationRoute and ApplicationGUID in the AppDelegate as well as look into the debug log in the Xcode console.

Screen Shot 2015-10-21 at 2.26.11 PM

Configure IBM Push Notifications on Bluemix

Now that the client has successfully connected to the Bluemix backend application, the IBM Push Notifications service can be configured through the Bluemix Dashboard.

a) In the Bluemix Application’s Overview page, click the IBM Push Notifications tab:
Screen Shot 2015-10-21 at 2.35.55 PM

b) A valid Apple Push Certificate (.p12 file) must be uploaded to the IBM Push Notification Dashboard in order to successfully send and receive Push Notifications. This can be accomplished in the Configuration tab of the Push Notification Dashboard:

Screen Shot 2015-10-21 at 2.38.07 PM

Click Choose File and navigate to the location of a valid APNs enabled .p12 file. Once the correct certificate has been highlighted click Open.

Input the corresponding password in the Password field and click Save. The certificate will be uploaded to the service and the corresponding details will be displayed:

Screen Shot 2015-10-21 at 2.46.27 PM

Note: Please verify that the certificate has not become expired by checking the Certificate Expire Date field.

Integrating IBM Push Notifications on the iOS Client

Now that the Bluemix application has been configured for Push Notifications, the client must be modified to handle notification subscription and reception. This can be accomplished by completing the following steps:

Integrate the IMFPush SDK into the iOS application by using CocoaPods
a) Open the Podfile which is included in the project downloaded from Github in the following location: bms-samples-ios-helloworld/{iOS_LANGUAGE}/Podfile
b) Add the IMFPush pod reference (pod ‘IMFPush’):
Screen Shot 2015-10-21 at 2.56.04 PM

c) Once the Podfile has been saved, a pod install must be executed in order to update the project with the new dependencies. Open a terminal window and run the following command inside the bms-samples-ios-helloworld/{iOS_LANGUAGE}/ directory:

pod install

Screen Shot 2015-10-21 at 3.06.39 PM

Note: If CocoaPods is not installed please follow the CocoaPods Getting Started Guide.

d) Again open up the helloWorld.xcworkspace file to launch the project in Xcode with the new dependencies.
e) Next add corresponding import directives into the application in order to start using the IMFPush SDK properly.

#import <IMFPush/IMFPush.h>

Note: For Objective-c this import statement should be added to the AppDelegate or wherever the SDK will be used, but for Swift just add it in the helloWorld-swift-Bridging-Header.h file.

Registering the Device
In order to receive notifications on the device, the application must first register with the Apple Push Notification Service (APNs).

  • First a token is needed from APNs in order to begin the process. To accomplish this add the following code snippet to the didFinishLaunchingWithOptions method in the AppDelegate:

Objective-c:

//initialize Push
[[UIApplication sharedApplication] registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound |UIUserNotificationTypeAlert |UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
    

Swift:

//initialize Push
let notificationType: UIUserNotificationType = [.Alert, .Badge, .Sound]
let notificationSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
application.registerUserNotificationSettings(notificationSettings)
application.registerForRemoteNotifications()
  • Now it is required to pass the token to Push Notifications in order to register successfully. Please add the following function to the AppDelegate to complete this:

Objective-c:

-(void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{

Swift:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        let push = IMFPushClient.sharedInstance()
        push.registerDeviceToken(deviceToken, completionHandler: { (response, error) -> Void in
            if error != nil {
               NSLog(("Error during device registration %@"), error.description)
            }
            else {
                 NSLog("Successfully registered for push Notification")
            }
        })
    }

Note: Currently the above code outputs a log message in the Xcode console showing the status of Push registration with an error message or a success message. It is possible to handle this in any manner that fits the application’s requirements, such as displaying an alert message or updating a label, etc.

Running and Testing the Sample

Now that the Bluemix application and client side HelloWorld application have been properly configured some tests should be conducted in order to make sure the application is working as expected.

Testing Client Side Registration
The newly configured HelloWorld application should be run on a physical device, through Xcode, in order to test the client side registration process.

-Once the physical iOS device is plugged in to the machine the target can be changed directly in Xcode to this device:

Screen Shot 2015-10-21 at 4.02.31 PM

-Choose the physical device as the target and run the application (Product->Run). Once the application is running an alert should appear asking permission to allow Push Notifications:

Screen Shot 2015-10-21 at 4.07.53 PM
-Click the OK button and check the Xcode console to verify the device has registered successfully:

2015-10-21 16:08:55.392 helloWorld[10082:3246172] Successfully registered for push Notification

Please Note: In order to successfully register a device and application to APNs the following requirements must be met to comply with Apple’s standards:

  • A valid APNs enabled Bundle Identifier must be obtained and used
  • A valid APNs enabled Provisioning Profile must be obtained and used
  • A valid Code Signing Identity and Developement Certificate must be obtained and used
  • The device used must be included as a part of the Provisioning Profile

To get more information on the above please read Apple’s Push Notification Service Guide.

Sending a Push Notification from the Bluemix Dashboard
Now that the client has successfully registered with the Push Notification service, the Bluemix Dashboard can be used to send a push notification to the device.

a) From the Bluemix Dashboard of the application previously created click the IBM Push Notifications tab under the Services section.

b) From the Notifications tab in the Push Dashboard, messages can be sent to devices with multiple options. To test sending a Push Notification to the iOS device that has been configured:
– Select Only iOS devices in the Choose The Audience field.
– Input desired text in the Message Text field.
– Input desired values in any of the Optional Fields.

Screen Shot 2015-10-22 at 1.29.51 PM

  • Click Send when the desired values have been entered.

Please note the client side application needs to be in the background on the device in order to receive the Push Notification successfully.

  • In the Bluemix Push Dashboard a verification message will be displayed showing the status of the notification:

Screen Shot 2015-10-22 at 1.35.28 PM

  • On the client side device the following notification should be seen:

Banner Notification:
Screen Shot 2015-10-22 at 1.40.44 PM

Lock Screen Notification:
Screen Shot 2015-10-22 at 1.41.00 PM

Congratulations your HelloWorld is now registered and configured to receive IBM Push Notifications.

To learn more about further capabilities of the Push Notification service including how to create/manage tags and subscriptions as well as handling other types of notifications please see the Getting started with Push Notifications Guide.

Other Resources:
IMFPush API Reference
Push REST API Documentation

Add Comment
One Comment

Leave a Reply

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


Gurdeep

Hi, is this code compatible with the latest version of BlueMix, in Jan-2017 ? First it was hard to App GUID, which I think is now called TenantID. Also found compilation errors in Xcode around configuring, IMFClient.sharedInstance().initializeWithBackendRoute(“”, backendGUID: “”)

The screenshots above do not seem to reflect the current UI in BlueMix, so wondering if this tutorial has tested recently ?

Thanks

Reply
More Community Stories

Latently, Bitfusion, and IBM Cloud enable democratized access to deep learning

Latently, Bitfusion, and IBM Cloud partnered to provide a deep learning development platform and best-in-class GPU infrastructure allowing you to publicly replicate all AI and ML scientific papers via the Latently Deep Learning Certificate (LDLC) program.

Continue reading

Lively chatbots: Best Practices

More and more chatbots are being developed and there are good reasons for it. Not all chatbot projects succeed. Often, missing user acceptance is stated. The dialog system might not have hit the nerve, might not have fitted into the target environment. Would you talk with a friend who does not remember your name is repeating the same five phrases over and over again? I would not. So what can be done to make chatbots more lively, more human-like? Here are some best practices and ideas on how to implement them.

Continue reading

WEBCAST: Bridging the Availability Gap with Veeam on IBM Cloud

“Veeam provides a low cost, simple and automated way to ensure your data is protected with recovery options locally or offsite in another IBM data center” – Zeb Ahmed, VMware on IBM Cloud Offering Manager

Continue reading