ibm-mfp-push

2.0.0 • Public • Published

IBM Bluemix Mobile Services - Cordova Plugin Push SDK

Cordova Plugin for the IBM Bluemix Mobile Services Push SDK

Installation

Installing necessary libraries

You should already have Node.js/npm and the Cordova package installed. If you don't, you can download and install Node from https://nodejs.org/en/download/.

The Cordova library is also required to use this plugin. You can find instructions to install Cordova and set up your Cordova app at https://cordova.apache.org/#getstarted.

Installing the Cordova Plugin for Bluemix Mobile Services Push SDK

Creating a Cordova application

  1. Run the following commands to create a new Cordova application. Alternatively you can use an existing application as well.

    cordova create {your_app_name}
    cd {your_app_name}
  2. Edit config.xml file and set the desired application name in the <name> element instead of a default HelloCordova.

  3. Continue editing config.xml. Update the <platform name="ios"> element with a deployment target declaration as shown in the code snippet below.

    <platform name="ios">
        <preference name="deployment-target" value="8.0" />
        <!-- add deployment target declaration -->
    </platform>
  4. Continue editing config.xml. Update the <platform name="android"> element with a minimum and target SDK versions as shown in the code snippet below.

    <platform name="android">
        <preference name="android-minSdkVersion" value="15" />
        <preference name="android-targetSdkVersion" value="23" />
        <!-- add minimum and target Android API level declaration -->
    </platform>

    The minSdkVersion should be above 15.

    The targetSdkVersion should always reflect the latest Android SDK available from Google.

Adding Cordova platforms

Run the following commands according to which platform you want to add to your Cordova application

cordova platform add ios
 
cordova platform add android

IMPORTANT: Make sure you use this iOS version for the cordova platform. It is required for the cordova app to build.

Adding the Cordova plugin

From your Cordova application root directory, enter the following command to install the Cordova Push plugin.

cordova plugin add bms-push

This also installs the Cordova Core plug-in, which initializes your connection to Bluemix.

From your app root folder, verify that the Cordova Core and Push plugin were installed successfully, using the following command.

cordova plugin list

Note: Existing 3rd party push notification plugins (e.g., phonegap) may interfere with ibm-mfp-push. Be sure to remove these plugins to ensure proper funcitonality.

Configuration

Configuring Your iOS Development Environment

  1. Follow the Configuring Your iOS Development Environment instructions from Bluemix Mobile Services Core SDK plugin

Updating your client application to use the Push SDK

By default, Cordova creates a native iOS project built with iOS, therefore you will need to import an automatically generated Swift header to use the Push SDK. Add the following Objective-C code snippets to your application delegate class.

At the top of your AppDelegate.m:

#import "[your-project-name]-Swift.h"

If your project name has spaces or hyphens, replace them with underscores in the import statement. Example:

// Project name is "Test Project" or "Test-Project"
#import "Test_Project-Swift.h"

Add the code below to your application delegate:

Objective-C:

// Register device token with Bluemix Push Notification Service
- (void)application:(UIApplication *)application
     didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
 
       [[CDVBMSPush sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
 
// Handle error when failed to register device token with APNs
- (void)application:(UIApplication*)application
     didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
 
      [[CDVBMSPush sharedInstance] didFailToRegisterForRemoteNotificationsWithError:error];
}
 
// Handle receiving a remote notification
-(void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
 
    [[CDVBMSPush sharedInstance] didReceiveRemoteNotificationWithNotification:userInfo];
}
 
// Handle receiving a remote notification on launch
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
 
    [[CDVBMSPush sharedInstance] didReceiveRemoteNotificationOnLaunchWithLaunchOptions:launchOptions];
}

Swift:

// Register device token with Bluemix Push Notification Service
func application(application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
 
    CDVBMSPush.sharedInstance().didRegisterForRemoteNotificationsWithDeviceToken(deviceToken)
}
 
// Handle error when failed to register device token with APNs
func application(application: UIApplication,
    didFailToRegisterForRemoteNotificationsWithError error: NSErrorPointer) {
 
    CDVBMSPush.sharedInstance().didReceiveRemoteNotificationWithNotification(error)
}
 
// Handle receiving a remote notification
func application(application: UIApplication,
    didReceiveRemoteNotification userInfo: [NSObject : AnyObject],  fetchCompletionHandler completionHandler: ) {
 
    CDVBMSPush.sharedInstance().didReceiveRemoteNotificationWithNotification(userInfo)
}
 
// Handle receiving a remote notification on launch
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
 
    CDVBMSPush.sharedInstance().didReceiveRemoteNotificationOnLaunchWithLaunchOptions(launchOptions)
}

Configuring Your Android Development Environment

Android development environment does not require any additional configuration. You can open the Android Project generated by Cordova in [your-app-name]/platforms/android directory with Android Studio or use Cordova CLI to build and run it.

Usage

The following BMSPush Javascript functions are available:

Javascript Function Description
initialize(pushAppGUID, clientSecret) Initialize the Push SDK.
registerDevice(options, success, failure) Registers the device with the Push Notifications Service.
unregisterDevice(success, failure) Unregisters the device from the Push Notifications Service
retrieveSubscriptions(success, failure) Retrieves the tags device is currently subscribed to
retrieveAvailableTags(success, failure) Retrieves all the tags available in a push notification service instance.
subscribe(tag, success, failure) Subscribes to a particular tag.
unsubscribe(tag, success, failure) Unsubscribes from a particular tag.
registerNotificationsCallback(callback) Registers a callback for when a notification arrives on the device.

Android (Native) The following native Android function is available.

Android function Description
CDVBMSPush. setIgnoreIncomingNotifications(boolean ignore) By default, push notifications plugin handles all incoming Push Notification by tunnelling them to JavaScript callback. Use this method to override the plugin's default behavior in case you want to manually handle incoming push notifications in native code.

Examples

Using BMSPush

Register for Push Notifications

 
// initialize BMSPush SDK
var appGUID = "Your Push service appGUID";
var clientSecret = "Your Push service clientSecret";
 
BMSPush.initialize(appGUID,clientSecret);
 
var success = function(response) { console.log("Success: " + response); };
var failure = function(response) { console.log("Error: " + response); };
 
 
// Register device for push notification without UserId
var options = {};
BMSPush.registerDevice(options, success, failure);
 
// Register device for push notification with UserId
var options = {"userId": "Your User Id value"};
BMSPush.registerDevice(options,success, failure);

You can access the contents of the success response parameter in Javascript using JSON.parse:

var token = JSON.parse(response).token
Available keys
token
userId
deviceId

To unregister for push notifications, simply call the following:

BMSPush.unregisterDevice(success, failure);

Retrieving Tags

In the following examples, the function parameter is a success callback that receives an array of tags. The second parameter is a callback function called on error.

To retrieve an array of tags to which the user is currently subscribed, use the following Javascript function:

BMSPush.retrieveSubscriptions(function(tags) {
    alert(tags);
}, failure);

To retrieve an array of tags that are available to subscribe, use the following Javascript function:

BMSPush.retrieveAvailableTags(function(tags) {
    alert(tags);
}, failure);

Subscribe and Unsubscribe to/from Tags

var tag = "YourTag";
BMSPush.subscribe(tag, success, failure);
BMSPush.unsubscribe(tag, success, failure);

Receiving a Notification

var handleNotificationCallback = function(notification) {
    // notification is a JSON object
    alert(notification.message);
}
 
BMSPush.registerNotificationsCallback(handleNotificationCallback);

The following table describes the properties of the notification object:

Property Description
message Push notification message text
payload JSON object containing additional notification payload.
sound The name of a sound file in the app bundle or in the Library/Sounds folder of the app’s data container (iOS only).
badge The number to display as the badge of the app icon. If this property is absent, the badge is not changed. To remove the badge, set the value of this property to 0 (iOS only).
action-loc-key The string is used as a key to get a localized string in the current localization to use for the right button’s title instead of “View” (iOS only).

Example Notification structure:

// iOS
notification = {
    message: "Something has happened",
    payload: {
        customProperty:12345
    },
    sound: "mysound.mp3",
    badge: 7,
    action-loc-key: "Click me"
}
 
// Android
notification = {
    message: "Something has happened",
    payload: {
        customProperty:12345
    },
    id: <id>,
    url: <url>
}

Release Notes

Copyright 2016-17 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package Sidebar

Install

npm i ibm-mfp-push

Weekly Downloads

2

Version

2.0.0

License

Apache version 2.0

Last publish

Collaborators

  • agirijak
  • bluemixmobileservices