The Push Notification feature of Kumulos allows you to send unlimited push notifications to installations of your app on iOS and Android devices, via the native Apple Push Notification Service (APNS) and Google Cloud Messaging (GCM/FCM). Please see the Push Notifications section of our docs for more details.

PushWoosh is another popular push notification vendor. If, for whatever reason, you wish to send a push notification via PushWoosh, then you can write a KScript method to do this.

Please note that any notifications sent via PushWoosh will not show up in Analytics or your Push Notification Dashboard. You must integrate Kumulos push notifications to get this.

Ingredients

This recipe assumes that you have KScripts enabled for your app, you have your own PushWoosh developer account and you have integrated the PushWoosh SDK into your client application.

You need a PushWoosh Developer account ($49 per month) to access their Remote API.

Recipe

Click the 'Create Some KScript' button and create a new KScript method called _push_ to encapsulate the PushWoosh integration. This method will define a Push object with three functions:

  • _callPushWoosh (a private method to interact with PushWoosh API)
  • _createNotification (a private method to create the payload)
  • _broadcast (a public method called to send push notification)
Push = {
   _pwApiKey: 'YOUR-PUSHWOOSH-API-KEY',
   _pwAppCode: 'YOUR-PUSHWOOSH-APP-CODE',
   _client: K.http.createClient('https://cp.pushwoosh.com'),
   _headers: {'Content-type': 'application/json'},

   _callPushWoosh: function (action, data) {
       K.log('Push._callPushWoosh');

       var result = 0;

       var json = K.JSON.stringify({
           request: data
       });

       try {
           var response = this._client.post(action, json, this._headers);
           result = response.isSuccess();

           if (result) {
               K.log('Success');
               K.log(response.getBody());
           }
           else {
               K.log('Failed');
           }
       }
       catch (e) {
           K.log(e);
       }

       return result;
   },

   _createNotification: function (content, devices, data) {
       K.log('Push._createNotification');

       var msg = {
           send_date: 'now',
           content: content
       };

       if (devices && devices.length) {
           msg.devices = devices;
       }

       if (data) {
           msg.data = data;
       }

       return {
           application: this._pwAppCode,
           auth: this._pwApiKey,
           notifications: [msg]
       };
   },

   // Send a push notification with message to named devices.
   // @param {[string]} devices - optional specify devices to push to, else all devices pushed to.
   // @param {object} extra  - optional extra data bundle (see pushwoosh API doc)
   broadcast: function (message, devices, extra) {
       K.log('Push.broadcast');

       var data = this._createNotification(message, devices, extra);
       return this._action('/json/1.3/createMessage', data);
   }
};

The broadcast function takes three parameters

  • message is the content of the push notification
  • devices is an array of PushWoosh tokens to send to. If omitted, push will be sent to all devices registered on your PushWoosh account.
  • extra is an optional custom data bundle

When your mobile application or web based control panel calls KScript methods to update your application data, you can include( _push_ ) and call Push.broadcast() to send a push notification to all devices registered to receive push notifications on your PushWoosh account.

include('_push_');
Push.broadcast('Hello World');

If you want to send a push notification to specific devices, then first you have to store PushWoosh device tokens in Kumulos. To get the PushWoosh device token, your application will need to:

  • Initialise PushWoosh SDK - this will return device token
  • Call a Kumulos API method to upload device token to Kumulos
  • Save this device token to a table in your Database

PushWoosh

Now, when you want to send a push notification to specific devices, you can read the device tokens from the database and pass them into the broadcast function:

include('_push_');
Push.broadcast('Hello World', [
    'TOKEN1',
    'TOKEN2'
]);

If your data is not being updated by KScript methods and you wish to send push notifications when data is updated, a KScript method can be scheduled to run once per hour or day. This could then use the timeCreated and timeUpdated magic fields to determine if there have been any changes to your data since last run to warrant sending a push notification etc.

Further reading

A full reference guide to the PushWoosh API service can be found on the PushWoosh website