IBM BlueMix is a beta-level product, and it will be changing as we continually make things better and easier to use. We'll do our best to keep this article up to date, but it will not always be perfectly aligned. Thanks for understanding.
You may already know about some of the benefits of IBM Bluemix™, IBM's open platform for developing and deploying mobile and web applications. The many pre-built services in Bluemix make it easy to build and enhance applications. In this article, we'll cover the steps for how to use JavaScript APIs to send a Push notification from your mobile app.
This article uses the open source Apache Cordova hybrid mobile application project. Apache Cordova enables mobile app developers to access native device functions through JavaScript. Cordova apps are written using standard web technologies, such as HTML, CSS, and JavaScript.
“As a mobile application developer, I want to get up and running with traditional native features like Push notifications as fast as possible. With open technologies like Apache Cordova and Cloud Foundry as part of IBM Bluemix, I found a way.”
Get the code
If you already have an existing hybrid mobile project, such as an IBM Worklight® project, skip this section.
cordova create bluemixpush com.example.bluemixpush BluemixPush
cd bluemixpush
ex. cordova platform add android
Note: If this is your first time developing a Cordova project for a specific platform, you need to perform some additional steps to set up the platform's development environment.
For example, to configure an Android environment, perform the following steps:
READ:Apache Cordova plugins
The Push service used in this article, along with many mobile cloud services provided as part of Bluemix, is available as a node packaged module. Install the ibmbaas package to initialize the Push service with the Bluemix mobile app.
bower install https://hub.jazz.net/git/mobilec/ibmbaas/.git
Install the IBM Push service from npm:
bower install https://hub.jazz.net/git/mobilec/ibmpush/.git
Successful installation of the Bluemix ibmpush package results in the following response.
Click to see larger image
Now that you have a hybrid application ready to go and your own IBM Push service installed from npm, you need to set up Bluemix to use the service:
On the mobile application information page in Bluemix for your new mobile app under Services, see the Push service bound to the application.
Click the Push service.
This Services section is your dashboard for all Push-related issues for your Bluemix app. You can even use this screen to send a test notification to registered devices.
Now that you have created a Bluemix app and saved the app ID, go back into development mode. Add the Bluemix Push JavaScript APIs to your hybrid mobile application to push messages:
<script src="bower_components/ibmpush/js/IBMPush.min.js"></script>
<script src="bower_components/ibmbaas/js/IBMBaaS.min.js"></script>
IBMBaaS.initializeSDK(__YOUR_APP_ID__);
var push = IBMPush.initializeService();
// Push Notification content var message = { alert : "Hello Bluemix World", url : "https://www.ng.bluemix.net" } // Send the notification push.sendBroadcastNotification(message).then(function(response) { alert("Push sent!" + JSON.stringify(response)); },function(err) { console.log(err); alert('error sending push message: ' + JSON.stringify(err)); });
For more complicated Push examples using the IBM Push JavaScript APIs, see the JavaScript API section of the IBM Mobile Cloud Services SDK Developer Guide.
Now you're ready to test the app. If you're using a pure Apache Cordova application, use the command line. For example, I am testing an Android application and using Eclipse to monitor the log output using LogCat.
cordova emulate android
If devices are registered, they are displayed in a table.
With the JavaScript SDK for Bluemix Mobile Backend as a Service Push APIs, it is not possible to register devices to receive Push messages; you must use the Android and iOS native Bluemix SDK.
To register a device with Push to view Push notifications, see "Extend an Android app using the Push cloud service," which includes a complete code example that can be downloaded and installed on your device to test the ability to receive Push messages.
If an error message indicates that the GCM & APNS credentials are missing, follow these steps to correct the problem:
Note: You can enter two different sets of credentials: one for Sandbox and another for Production. It is easy to toggle the service mode between Sandbox and Production when you're ready to switch.
You can now add Bluemix Push capabilities to an existing or new hybrid mobile application using the npm ibmpush module and available JavaScript APIs.