These docs are for v1.0. Click to read the latest docs for v2.0.

Coexistence with 3rd party JS

Introduction

Smartech JS has the capablity to coexist with JS of other vendors/client. This will help the client to not only send WPNs from their platform / other vendor but also at the same time use Smartech for sending WPNs.

For coexistence it is mandatory to put Smartech service worker along with client's/vendor's service worker in any case. Smartech supports only FCM and this holds true in case of Coexistence as well.

Smartech base JS integration is mandatory

After Smartech base JS is integrated Smartech will provide a Javascript method to get a combination of GUID, token and user identity(if exists).
If base JS integrated it will capture Tokens and all the events.

The first step to be taken for enabling coexistence is to 1. head over to the Asset section 2. View/edit the integrated Website 3. Enable Coexist.

1172

Sending and Receiving WPN tokens

There are 2 cases:

1.For Smartech to receive tokens ,
Client needs to add the following code snippet in their existing JS:

<script>
  var token = TOKENVALUE;
  smartech('set', 'token', token).then(function(response) {
     console.log(response); // for debugging
     //insert your business logic here
     })
</script>

2.Smartech to send tokens,to 3rd party
The following code snippet is to be used :

<script>
 
  smartech('get', 'token');
  //ex
  smartech('get', 'token').then(function(userToken) {
     console.log(userToken); // for debugging
     //insert your business logic here
  })
</script>

Sending Web push notifications

There will be 2 cases for sending web push notifications

  1. The client will be sending WPN though smartech
  2. The client will be sending WPN though 3rd party tool

Sending WPN though smartech

While sending WPN from Smartech, we will introduce a new keyword 'origin = smartech' and then in our payload, we will check if this keyword exists with value or not. Then we will handle click, close, delivered events and take decisions whether to render WPN for the user from Smartech.

if the above-mentioned keyword doesn't exist, Netcore will ignore the push events and hence in such a case, the client will then be able to send WPN from their side along with the above-mentioned events.

Sending WPN though 3rd party

While sending WPN from 3rd party, the Client will check if keyword 'Origin = Smartech exists in the payload, with value or not.

if the above-mentioned keyword exists, the client will ignore the push events and hence in such a case, Netcore will then be able to send WPN from their side along with the above-mentioned events.

Implementation of WPN

To implement this feature following code snippet needs to be placed at client's sw.js to handle BPN events.

1. For push Event :

var pushOrigin = validatePushOrigin(notificationPayload);
        if (pushOrigin === true) {
            Promise.resolve();
            return;
        }

2. For notification click Event :

var pushOrigin = validatePushOrigin(notificationPayload);
        if (pushOrigin === true) {
            Promise.resolve();
            event.notification.close();
            return;
        }

3. For notification close Event:

var pushOrigin = validatePushOrigin(notificationPayload);
        if (pushOrigin === true) {
            event.notification.close();
            return;
        }

4. Need to place below function in client's sw.js file to check the origin.

function validatePushOrigin(payload) {
            if (payload.origin && payload.origin === 'smartech') {
                return true;
            }
            return false;
       }

5. To export token to smartech platform following code needs to be added when user does opt-in activity on website.

/**
         * This function will accept given token and will sent to smartech platform
         * @returns {promise}
         */
        smartech('set','token', 'TOKENVALUE').then(function(response){
            console.log(response);
            //client's business logic will goes here if required

        });