Push notifications

The Layer Push Notification Service can be used to keep your app’s data updated.

Setting up push with Layer

The Layer Web SDK currently does not support push notifications for Desktop; however, if running a Hybrid Application (Cordova, Phonegap, Ionic, etc…), there are APIs for registering for push notifications.

Before trying to receive notifications, you need to read the IOS or Android guide for setting up Google/Apple push services, as well as configuring push within the Developer Dashboard.

Once this is done, you will also need to select a suitable Push Plugin for your environment, such as phonegap-plugin-push, and will need to follow its instructions for getting your Device ID, and other information needed to use Layer’s APIs to signup for push notifications.

There are three APIs around Push Notifications:

function initCordovaPush(client, scopeIn) {
  function registerForPush() {
    client.registerAndroidPushToken({
      token: registrationId,
      deviceId: window.device.uuid,
      senderId: String(SENDERID)
    }, function(err) {
      if (err) console.error('Error registering for Push Notifications with Layer:', err);
      else console.log('Push Notifications registered on Layer Servers');
    });
  }

  document.addEventListener('deviceready', function() {
    var push = PushNotification.init({
      android: {
        senderID: String(SENDERID),
        vibrate: true
      }
    });

    push.on('registration', function(data) {
        registrationId = data.registrationId;
        if (client.isReady) registerForPush();
        else client.on('ready', registerForPush);
    });
  });
}

In the example above:

  • SENDERID comes from our Google Apps configuration
  • The PushNotification Phonegap plugin gathers the registrationId
  • Phonegap/other plugins provide window.device.uuid
  • registerAndroidPushToken sends the request that provides Layer’s servers with everything needed to send notifications to your device.

Desktop Notifications

While Push services are not currently supported on desktops, as long as your application is running, it can still receive events sent via websocket, and can use Desktop Notifications to notify users of new Messages.

The Layer Client will trigger a messages:notify event any time there is a Message for which a desktop notification is considered appropriate (i.e. newly arrived via websocket). Below is some example code for handling these events:

/**
 * Web notifications using notifyjs
 * https://github.com/alexgibson/notify.js
 */

const Notify = require('notifyjs').default;

const ICON = 'https://s3.amazonaws.com/static.layer.com/logo-only-blue.png';
const TIMEOUT_SECONDS = 6;

if (window.Notification) {
    Notify.requestPermission(function() {
        layerClient.on('messages:notify', function(evt) {
            try {
              const message = evt.message;
              let clickHandler;
              if (message instanceof layer.Announcement) {
                clickHandler = function() {
                  showAnnouncements(message);
                }
              } else {
                clickHandler = function() {
                  showConversation(message.getConversation());
                };
              }

              const textParts = message.parts.filter(function(part) {
                return part.mimeType === 'text/plain';
              });
              const text = textParts.map(function(part) {return part.body;}).join(' ');
              new Notify('New Layer ' + (message instanceof layer.Announcement ? 'Announcement' : 'Message'), {
                icon: ICON,
                timeout: TIMEOUT_SECONDS,
                body: message.sender.displayName + (text ?  ': ' + text : ' sent a message'),
                tag: message.conversationId,
                notifyClick: clickHandler
              }).show();
            } catch(e) {
              // do nothing
            }
        });
    });
};

Note that lookupDisplayName is in the code above on the assumption that your User ID is not a displayable name, and that you need to lookup a displayable name from the User ID.

Synchronization Deleting