PushNotifications example


The index.html file containing JavaScript that makes use of the PushNotifications API functionality is listed below.

<!DOCTYPE html>
<html manifest="manifest.appcache">
<head>
  <title>Push notifications API usage example</title>
  <script type="text/javascript">

  // configuration flag about automatic token passed to Management Console over ACF messaging
  var PUSHES_FROM_MC_CONSOLE_MODE = true;

  var pushNotificationCallback = {
    onPushNotification: function(data) {
      printText("Push Notification data: " + data);
    }
  };

  function printText(str) {
    var d = document.getElementById('text-box');
    d.appendChild(document.createTextNode(str));
    d.appendChild(document.createElement('br'));
    d.scrollTop = d.scrollHeight;
  }

  function printKeyValuePair(key, value) {
    printText(key + " : " + value);
  }

  function PNregister() {
    var initACF = function() {
      printText("ACF initialization");
      var callback = {
        onConnectionStatusChanged: function(status) {
            printText("onMessagingConnectionStatus: " + status);
        }
      };
      launchbox.ACF.addListener(callback);
      launchbox.ACF.start();
    };

    var callback = {
      onRegistrationSucceeded: function(token) {
        printText("Push token: " + token);
        if (PUSHES_FROM_MC_CONSOLE_MODE) {
          initACF();
        } else {
          printText("Push token should be now passed to custom push notifications server.");
        }
      },
      onRegistrationFailed: function(failureType) {
        printText("Push register failed")
      }
    };
    launchbox.PushNotifications.register(callback);
  }

  function PNadd() {
    printText("Push Notification Listener Added");
    launchbox.PushNotifications.addListener(pushNotificationCallback);
  }

  function PNremove() {
    printText("Push Notification Listener Removed");
    launchbox.PushNotifications.removeListener(pushNotificationCallback);
  }

  window.onLaunchboxLoaded = function() {
    printText('HC API loaded');
  };
  </script>
</head>
<body>
  <header>
    <h3><span>Push Notifications API usage example</span>
    </h3>
  </header>
  <input type="button" onClick="PNadd();" value="Set Push Notification listener">
  <input type="button" onClick="PNregister();"
    value="Register the application for Push Notifications">
  <input type="button" onClick="PNremove();" value="Remove Push Notification listener">

  <div id="text-box"></div>
</body>
</html>

The contents of the cache manifest file called manifest.appcache for this application are listed below:

CACHE MANIFEST

CACHE:
index.html

NETWORK:
*

The webapp-descriptor.xml file for this application is defined in the following way:

<?xml version="1.0" encoding="UTF-8"?>
<webapp-descriptor xmlns="http://www.pega.com/application-hosting/
web-app-descriptor/2.0">
  <appkey>01234567-89ab-cdef-0123-456789abcdef</appkey>
  <version>1.0.0</version>
  <name>Push notifications API usage example</name>
  <author>Pegasystems</author>
</webapp-descriptor>

Related topics

Public API reference
PushNotifications
Legal notice | Copyright © 2016 and Confidential to Pegasystems Inc. All rights reserved
PDN | Hybrid Container | Feedback
Advanced...