Cordova

The JavaScript App Messaging Foundation SDK can be incorporated into a Cordova app.

There is no specific Cordova plugin; all the necessary functionality required to configure push notifications is present in the core SDK. You just need to install the SDK into your app.

Learn how in Install the SDK. For Ionic 2, follow the NPM guidelines. For Ionic 1 or similar, follow the Bower guidelines.

Safe list the SDK's API calls

The built-in security in Cordova based apps restrict the URIs the Cordova app pages can access, therefore you must safe list the URIs the SDK uses in order for it to operate.

To do this:

  • 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:*

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 require a push notification plugin. The SDK implements push in a standard way so you can just install phonegap-plugin-push, or something similar.

Below are some snippets that show how to send the registrationId to the system through the SDK. Retrieving the push registrationId is an asynchronous task that is 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 the two tasks have been split apart, and the registrationId is saved to localStorage for the purposes of this snippet.

Get the registrationId

This snippet uses the Push plugin from Ionic Native to retrieve the registrationId and setup a notification handler.

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

Send the push registrationId

This snippet shows how to send the registrationId to the system through the SDK.

The assumption is that you have an initialised SDK and a valid session at this point. Note the Environment import.

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 ;-(");
}

📘

Push Payloads

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