Cordova

Cordova Integration

The Javascript App Messaging Foundation SDK can easily be incorporated into a Cordova app.
There is no specific Cordova plugin as such as all the necessary functionality required to configure push notifictions are present in the core sdk. You just need to install the SDK into your app.

See the Installation section for details of how to do this.

For Ionic 2, follow the NPM guidelines.

For Ionic 1 or similar, follow the Bower guidelines.

White listing the SDKs API calls

The built in security in Cordova based apps will restrict the URIs the Cordova app pages can access, therefore you will need to white list the URIs the SDK uses in order for it to operate. To do this do the following:

If using the cordova-plugin-whitelist

Ensure the following line is added to your config.xml file in your project:

<allow-navigation href="https://*.comapi.com/*" />

If using Content-Security-Policy tags

Ensure your Content-Security-Policy tags include the following directive:

connect-src https://api.comapi.com:*

For example:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;connect-src https://api.comapi.com:*">

Push plugins

You will need a push notification plugin. The SDK implements push in a standard way so you can just install phonegap-plugin-push or use something similar.

Here are some snippets showing how to send the registrationId to the system via the SDK.

Retrieving the push registrationId is an asynchronous task that will be performed AFTER the cordova deviceready event has fired.
This needs to be passed to the system and can only be passed after the SDK is initialised and a valid session has been created.
There is potential for a race condition here so i will split the two tasks apart and save the registrationId to localStorage for the purposes of this snippet.

Getting the registrationId

This snippet is using the Push plugin from Ionic Native to retrieve the registrationId (native push token) and setup a notification handler. There is no dotdigital code in this example:

import { Push } from 'ionic-native';

platform.ready().then(() => {

    let push = Push.init({
        ios: {
        alert: "true",
        badge: true,
        sound: 'false'
        }
    });

    push.on('registration', (data) => {
        console.log("got a registrationId", data.registrationId);
        localStorage.setItem("registrationId", data.registrationId);
    });

    push.on('notification', (data) => {
        console.log("got a pushNotification", data);
    });
    
}

Sending the push registrationId

This snippet shows how to send the registrationId to the system via the SDK.
The assumption is that you have an initialised SDK and a valid session at this point.
Note the Environment import for ES6 code.

import { Environment } from "@comapi/sdk-js-foundation";

// Put this somewhere appropriate in your app (at the end of you initialisation/login flow)

let registrationId = localStorage.getItem("registrationId");
// skip if registrationId hasn't been collected
if(registrationId){

    // There are separate methods to call depending on platform ...
    if (platform.is('ios')) {

        // You will need to create an APNS cert. in the apple developer portal.
        // Then you must upload it to your API space in the portal.
        // Can be a development or production cert, hence the environment parameter
        sdk.device.setAPNSPushDetails(">>> My Bundle Id <<<", Environment.development, registrationId)
        .then(result => {
            console.log("setAPNSPushDetails() succeeded", result);
        })
        .catch(error => {
            console.error("setAPNSPushDetails() failed", error);
        });

    }else if(platform.is('android')){

        sdk.device.setFCMPushDetails(">>> My Package Name <<<", registrationId)
        .then(result => {
            console.log("setFCMPushDetails() succeeded", result);
        })
        .catch(error => {
            console.error("setFCMPushDetails() failed", error);
        });

    }
}else{
    console.error("no registrationId ;-(");
}
// Put this somewhere appropriate in your app (at the end of you initialisation/login flow)

let registrationId = localStorage.getItem("registrationId");
// skip if registrationId hasn't been collected
if(registrationId){

    // There are separate methods to call depending on platform ...
    if (platform.is('ios')) {

        // You will need to create an APNS cert. in the apple developer portal.
        // Then you must upload it to your API space in the portal.
        // Can be a development (0) or production (1) cert, hence the environment parameter
        sdk.device.setAPNSPushDetails(">>> My Bundle Id <<<", 0, registrationId)
        .then(result => {
            console.log("setAPNSPushDetails() succeeded", result);
        })
        .catch(error => {
            console.error("setAPNSPushDetails() failed", error);
        });

    }else if(platform.is('android')){

        sdk.device.setFCMPushDetails(">>> My Package Name <<<", registrationId)
        .then(result => {
            console.log("setFCMPushDetails() succeeded", result);
        })
        .catch(error => {
            console.error("setFCMPushDetails() failed", error);
        });

    }
}else{
    console.error("no registrationId ;-(");
}

Push Payloads

When you send a message, you can individually craft the platform specific payloads.
They will be received in the notification event handler shown in the first snippet.