Skip to content

drcariel/bms-clientsdk-cordova-plugin-push

 
 

Repository files navigation

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.

Video tutorials

Below videos demonstrate how to install and use the Cordova Plugin for the IBM Bluemix Mobile Services Push SDK in iOS and Android applications.

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@3.9.0 

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 ibm-mfp-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

Configuration

Configuring Your iOS Development Environment

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

  2. Uncomment the following Push import statements in your bridging header. Go to [your-project-name]/Plugins/ibm-mfp-core/Bridging-Header.h:

//#import <IMFPush/IMFPush.h>
//#import <IMFPush/IMFPushClient.h>
//#import <IMFPush/IMFResponse+IMFPushCategory.h>

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{

	 [[CDVMFPPush sharedInstance] didRegisterForRemoteNotifications:deviceToken];
}
    
// Handle error when failed to register device token with APNs
- (void)application:(UIApplication*)application
	 didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {

	[[CDVMFPPush sharedInstance] didFailToRegisterForRemoteNotifications:error];
}
    
// Handle receiving a remote notification
-(void)application:(UIApplication *)application 
	didReceiveRemoteNotification:(NSDictionary *)userInfo 
	fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

	[[CDVMFPPush sharedInstance] didReceiveRemoteNotification:userInfo];
}

// Handle receiving a remote notification on launch
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

	[[CDVMFPPush sharedInstance] didReceiveRemoteNotificationOnLaunch:launchOptions];
}

Swift:

// Register device token with Bluemix Push Notification Service
func application(application: UIApplication, 
	didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

	CDVMFPPush.sharedInstance().didRegisterForRemoteNotifications(deviceToken)
}

// Handle error when failed to register device token with APNs
func application(application: UIApplication, 
	didFailToRegisterForRemoteNotificationsWithError error: NSErrorPointer) {
        
	CDVMFPPush.sharedInstance().didFailToRegisterForRemoteNotifications(error)
}
    
// Handle receiving a remote notification
func application(application: UIApplication, 
	didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 	fetchCompletionHandler completionHandler: ) {
	
	CDVMFPPush.sharedInstance().didReceiveRemoteNotification(userInfo)
}

// Handle receiving a remote notification on launch
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
	CDVMFPPush.sharedInstance().didReceiveRemoteNotificationOnLaunch(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 MFPPush Javascript functions are available:

Javascript Function Description
registerDevice(settings, 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
CDVMFPPush. 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.

Sequence Diagrams

image

image

Examples

Using MFPPush

Register for Push Notifications

var settings = {
	ios: {
		alert: true,
		badge: true,
		sound: true
	}
}
    
var success = function(response) { console.log("Success: " + response); };
var failure = function(response) { console.log("Error: " + response); };
    
MFPPush.registerDevice(settings, success, failure);

The settings structure contains the settings that you want to enable for push notifications. You must use the defined structure and should only change the boolean value of each notification setting.

Android does NOT make use of the settings parameter. If you're only building Android app, pass an empty object, e.g.

MFPPush.registerDevice({}, 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:

MFPPush.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:

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

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

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

Subscribe and Unsubscribe to/from Tags

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

Receiving a Notification

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

MFPPush.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 2015 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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 40.4%
  • Java 29.9%
  • JavaScript 17.0%
  • Objective-C 12.7%