<!---
   Licensed Materials - Property of IBM

   (C) Copyright 2016 IBM Corp.

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

# IBM MobileFirst Platform Foundation Push Plugin

Cordova Plugin for the IBM Mobile First Platform 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/](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](https://cordova.apache.org/#getstarted).

### Creating a Cordova application

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

	```
	$ cordova create {appName}
	$ cd {appName}
	```

### Adding Cordova platforms

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

```Bash
cordova platform add ios

cordova platform add android

cordova platform add windows
```

### Editing config.xml

1. Edit `config.xml` file and set the desired application name in the `<name>` element instead of a default HelloCordova.

	
2. Continue editing `config.xml`. Update the `<platform name="android">` element with a minimum and target SDK versions as shown in the code snippet below.

	```XML
	<platform name="android">
		<preference name="android-minSdkVersion" value="15" />
		<preference name="android-targetSdkVersion" value="23" />
		// other properties
	</platform>
	```

	> The minSdkVersion should be above 15.
	
	> The targetSdkVersion should always reflect the latest Android SDK available from Google.

### Adding the Cordova plugin

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

```Bash
cordova plugin add cordova-plugin-mfp-push
```

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

```Bash
cordova plugin list
```


#### Updating your client application to use the Push SDK

### Configuring Your iOS Development Environment

You can open the iOS Project generated by Cordova in [your-app-name]/platforms/ios directory with XCode or use Cordova CLI to build and run it.

### Configuring Your Android Development Environment

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.

### Configuring Your Windows Development Environment

You can open the Windows Project generated by Cordova in [your-app-name]/platforms/windows directory with Visual Studio or use Cordova CLI to build and run it.


## Usage

The following MFPPush Javascript functions are available:

Javascript Function | Description
--- | ---
registerDevice(success, failure) | Registers the device with the Push Notifications Service.
unregisterDevice(success, failure) | Unregisters the device from the Push Notifications Service
getSubscriptions(success, failure) | Retrieves the tags device is currently subscribed to
getTags(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. 

## Examples

### Using MFPPush

#### Register for Push Notifications

```

var success = function(message) { console.log("Success: " + message); };
var failure = function(message) { console.log("Error: " + message); };
    
MFPPush.registerDevice(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);
```

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.getSubscriptions(function(tags) {
	alert(tags);
}, failure);
```
    
To retrieve an array of tags that are available to subscribe, use the following Javascript function:

```
MFPPush.getTags(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(notif);
}

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>
}

// Windows
notification = {
	message: "Something has happened",
	payload: {
		customProperty:12345
	},
	settings: { wns:{
    raw: {payload:{custom:"Push Message from MFP"}},
    badge: { value: "10"}}}
}
```
For details of the changes in this latest release, see http://www-01.ibm.com/support/docview.wss?uid=swg2C7000003