Notifications example


A snippet below contains JavaScript that makes use of the Notifications API functionality.

window.onLaunchboxLoaded = function() {
  // To receive the initial notification, attach a handler in onLaunchboxLoaded().
  launchbox.Notifications.onNotificationReceive.bind(notificationHandler);

  // Create a category for receiving new mails with Reply and Snooze buttons.
  var mailCategory = new NotificationCategory('mail');
  mailCategory.setActions(
    new NotificationAction('reply', 'Reply'),
    new NotificationAction('snooze', 'Snooze')
  );

  // Categories can be registered at any moment before notification is received.
  // It doesn't have to be done inside onLaunchboxLoaded().
  launchbox.Notifications.registerCategory(mailCategory);

  // Registration can be done at any time, but both Apple and Google recommend doing it on every app start.
  launchbox.Notifications.registerForPushNotifications()
    .then(function(token) {
      ...
    });
};

// Sample handler.
var notificationHandler = function(type, payload, actionId) {
  if (actionId === "reply") {
    ...
  } else if (actionId === "snooze") {
    ...
  }
};

To handle silent push notifications you can bind to the onNotificationReceive event. You should then check the payload for the appropriate value of the content-available field:

launchbox.Notifications.onNotificationReceive.bind(type, notification) {
  if ((notification.aps && notification.aps['content-available'] == 1) ||
      (notification.c2dm && notification.c2dm['content-available'] == 1)) {
      // Handle silent push
  }
};

Related topics

Notifications
Legal notice | Copyright © 2017 and Confidential to Pegasystems Inc. All rights reserved
PDN | Pega Mobile Client | Feedback
Advanced...