View Categories

Business Messenger Webpush – SDK

13 minutes of reading

Supports the following browsers caniuse.com/push-api.

Prerequisites
To proceed with the integration of HPRO Web Push SDK to your HTTPS website you must meet the following conditions:

  • Locate your formUuid from the SMS.es Business Messenger Application,
  • Download or use HPRO webpush SDK.

To use our Webpush, your site needs to have SSL certificate or you can use localhost domain during development.

1. Introduction #

Webpush SDK is a javascript library that provides subscription mechanism for Webpush notifications for your website/domain.

After a visitor subscribes you will be able to notify him with updates using standard the Business Messenger campaigns.

The SDK can be configured either as a small subscription button widget or used directly from javascript so that you can build your own custom subscription forms and collect some other information to be populated per contact in the Business Messenger Contact Manager.

2. Requirements #

  • Register your SMS.es account and confirm it.
  • Gain access over the Business Messenger Application by contacting your administrator.
  • In the Business Messenger Application go to the Web > Webpush section, create a Webpush Rule and find SDK Parameters for your Rule (UUID and Webpush Public Key) which you will use later in configuring the SDK.
SDK parameters

3. Setup #

Download SDK and Service worker files and put them on your server. You can download them separately or download hpro-wp.zip and extract it.

VersionBundleSDKService Worker
1.3.0hpro-wp.ziphpro-wp-sdk.min.jshpro-wp-sw.min.js
1.2.0hpro-wp.ziphpro-wp-sdk.min.jshpro-wp-sw.min.js
1.1.0hpro-wp-sdk.min.jshpro-wp-sw.min.js
1.0.0hpro-wp-sdk.min.jshpro-wp-sw.min.js

Include SDK script in your page:

index.html

3.1 Service Worker #

Standalone

If you have no service worker defined, you can use our SDK as a standalone solution:

Put our hpro-wp-sw.min.js in the root of your website.

Existing

Edit your service-worker.js and import a script which you have downloaded.

service-worker.js

3.2 SDK #

Initialize the Webpush SDK:

formUuid (YOUR_FORM_UUID) and applicationServerKey (YOUR_WEBPUSH_PUBLIC_KEY) parameters need to be replaced with real values for your Opt-in/Opt-out Rule from the Web > Webpush section of the Business Messenger app.

3.3 Webpush on Apple Mobile Devices (iOS, iPadOS) #

Starting with iOS/iPadOS 16.4, Apple supports standard webpush notifications, but there are some specific steps required to get it working: tst

  1. Home Screen Web Apps: Notifications only work if your website is started as a so-called “Home Screen web apps”.
  2. Manifest File:
  3. JavaScript Functions:
    • Your javascript logic and content should be adopted to support iOS use cases. You can use these helper function to achieve this:function isIOS() { const userAgent = navigator.userAgent || navigator.vendor || window.opera; return /iPad|iPhone|iPod/.test(userAgent) && !window.MSStream; } function isRunningAsWebApp() { return window.navigator.standalone === true; }
    • Then implement a similar flow (pseudo code ahead):let ios = isIOS() if (!ios || isRunningAsWebApp()) { // ... init webpush sdk normally } else if (ios){ // present a banner to invite iOS/iPadOS users to add your webapp to the Home Screen and run from it, here are some UI ideas https://web.dev/articles/promote-install }

The iOS visitor is expected to:

  • Visit your website on your Safari browser on a mobile Apple device on 16.4+
  • Click on the “Share” button of their Safari browser on a mobile device.
  • Click on the “Add to home screen” option.
  • Save the app on their device.
  • Open it the app from the home screen.
  • Subscribe to notifications (they must click a subscribe button before a native permission prompt can be displayed).

4. Usage #

Our SDK is defined in mind to be compatible with the JS Array structure.
Since SDK may not be available yet at the time of the calling method, Arrays allow us to postpone calling of those function when page is ready.
Every method is pushed to the array and executed in FIFO method. This is why you always use the .push method as a wrapper of set of methods.

Our SDK methods can be called in two ways.

4.1 Async Call #

General usage:

Without Promise, and with callback arguments:

4.2 Sync Call #

General usage:

Method specific usage with callbacks:

Method specific usage with Promise, without callbacks:

5. Configuration #

Properties:

PropertyTypeUsageDefaultValuesDescription
applicationServerKeyStringmandatoryVAPID key
apiUrlPrefixStringoptionalhttps://api.YOUR_BM_DOMAIN/news/v1Example:
https://api.sms.es
apiEndpointFormatStringoptional/forms/{formUuid}/activitiesExample:
/services/{serviceUuid}/forms/{formUuid}/activities
apiTestEndpointFormatStringoptional/forms/{formUuid}/test
identifierStringoptionalnull | undefinedIf you can identify the user with contact UUID, before SDK initialization, you can set it up here.
serviceUuidStringmandatoryIf used, provide the BM account UUID.
formUuidStringmandatoryForm UUID that is used to identify the subscription.
workerFileStringmandatoryservice-worker.jsFile name of the service worker.

Example:
service-worker.js
dataObjectoptional{}Custom data to send.
headersObjectoptional{}Custom headers to send.
whenUnsubscribedStringoptionalkeep_identitykeep_identity

remove_identity
keep_identity or remove_identity

Default: keep_identity

By default, when unsubscribe is finished, in local storage, identifier exist. After subscription, contact will be updated.

When remove_identity is set, after unsubscribe local storage identifier will be deleted. After subscription, new contact will be created.
autoSubscribeBooleanoptionalfalseBy default when SDK use notifyButton – autoSubscribe is true, in advanced mode is false

added in version 1.1.0
logEnableBooleanoptionalfalseEnable log which give use detailed subscription info

added in version 1.1.0
notifyButtonObjectoptionalnull | undefined | falsePropertyTypeDescriptionenableBooleanhideAfterSubscribeBooleanIf user is subscribed auto hide button.tooltipObjectText when hover on button text.PropertyTypeDefault ValuesubscribeStringSubscribe to notificationsunsubscribeStringUnsubscribe to notificationsnotificationObjectNotification info text after subscription success/error.PropertyTypeDefault ValuesubscribeStringYou are successfuly subscribed to notificationsunsubscribeStringYou have successfuly unsubscribederrorStringSubscribed to notifications faileddialogObjectDialog question when try to subscribe/unsubscribePropertyTypeDefault ValuesubscribeStringSubscribe to notificationsunsubscribeStringUnsubscribe from notificationspositionString or ObjectStringObject keystop-lefttoptop-rightleftbottom-leftrightbottom-rightbottom

Usage:

6. Methods #

6.1 init #

Call this function from your page where you want to use hproWebPush.

  • Do not call this method twice
  • This call is required before any other method is called

Properties:

PropertyTypeUsageDescription
configObjectmandatoryConfiguration
successCallbackFunctionoptionalSuccess Callback
errorCallbackFunctionoptionalError Callback

Return:
Promise

Usage:

Methods which are listed in documentation, can be used after init method is called and hproWebPush object is created.

6.2 subscribe #

The method does a subscription process.

Properties:

PropertyTypeDescription
contactDataObject
customHeaderObject
successCallbackFunctionPromise resolving.

successCallback(successResponse)

successResponse:subscribed
When a successful subscription is done.
errorCallbackFunctionPromise rejection.

errorCallback(errorResponse)

errorResponse:denied
When notification permission state is denied.notPermitted
All other conditions.

Return:
Promise

Usage:

6.3 isSubscribed #

The method checks to see if you are already subscribed. Each callback will return the permission state.

Properties:

PropertyTypeDescription
successCallbackFunctionPromise resolving.

successCallback(successResponse)

successResponse:
Resolves with notification permission status.
errorCallbackFunctionPromise rejection.

errorCallback(errorResponse)

errorResponse:
Rejects with notification permission status.

Return:
Promise

Usage:

6.4 update #

The method updates current subscription.

Properties:

PropertyTypeDescription
contactDataObject
customHeadersObject
successCallbackFunctionPromise resolving.

successCallback(successResponse)

successResponse:updated
When a successful subscription is done.
errorCallbackFunctionPromise rejection.

errorCallback(errorResponse)

errorResponse:denied
When notification permission state is denied.notPermitted
All other conditions.

Return:
Promise

Usage:

6.5 permit #

The method triggers the browsers notification permission dialog.

Properties:

PropertyTypeDescription
successCallbackFunctionPromise resolving.

successCallback(successResponse)

successResponse:
Resolves with notification permission status.
errorCallbackFunctionPromise rejection.

errorCallback(errorResponse)

errorResponse:
Rejects with notification permission status.

Return:
Promise

Usage:

6.6 unsubscribe #

The method unsubscribes current subscription.

Properties:

PropertyTypeDescription
contactDataObject
customHeadersObject
successCallbackFunctionPromise resolving.

successCallback(successResponse)

successResponse:unsubscribed
When a successful unsubscribe is done.
errorCallbackFunctionPromise rejection.

errorCallback(errorResponse)

errorResponse:
Rejects with notification permission status.

Return:
Promise

Usage:

7. Examples #

7.1 Contact Data and Auto Identifier #

In this example we only require End-User to enter his name for subscription.

The end result is that after successful subscription user is provided with identifier which will be saved for later use in browser’s local storage.

When subscription token is expired and identifier exists in browser’s local storage, SDK can identify and it will update existing contact.

7.2 Existing Identifier #

In this example we force SDK to use existing contact identifier (contact UUID). E.g. if you are building a user portal, you should store BM contact UUID along with other user profile information. This way you will be able to specify it in Webpush SDK parameters.

We have already identified the contact and we have initialized SDK with his identifier.

Identifier will also be saved for later use in browser’s local storage.

The end result is that after successful subscription the same user is updated with latest subscription token.

When subscription token is expired, SDK can identify and refresh subscription token.

7.3 Auto Subscribe #

Auto Subscribe will only work when user has given browser notification permissions and token has expired at some point.

It will not work on first user visit, because user needs to click on notification button to be asked for browser notification permissions, and grant them.

This all happens during the init procedure.

On every later visit user will be updated with the new subscription token.

It works in both cases: existing contact identifier or auto contact identifier.

8. Contact Data #

“Contact Data” is data that you can send to our service when subscribe method is triggered. With that way of gathering data, you will have more information about your subscriber.

KeyTypeValuesDescription
first_nameStringFirst Name
last_nameStringLast Name
nick_nameStringNick Name
second_nameStringSecond Name
genderStringmale
female
Gender
Female or male
birthdayStringBirthday
Format: 2018-01-03
salutationStringSalutation
faxStringFax
languageString
(lowercase)
en.wikipedia.org/wiki/List_of_ISO_639-1_codesLanguage
Example: ‘en’ for English
nationalityString
(lowercase)
en.wikipedia.org/wiki/ISO_3166-1Nationality
Example: “GB” for United Kingdom
mobileStringMobile
Example: +41 78 123 45 67
phoneStringPhone
Example: +41 78 123 45 67
titleStringTitle
zipStringZIP
addressStringAddress
cityStringCity
regionStringRegion
countryString
(uppercase)
en.wikipedia.org/wiki/ISO_3166-1country
Example: “GB” for United Kingdom
b_addressStringBusiness address
b_cityStringBusiness city
b_companyStringCompany name
b_countryString
(uppercase)
en.wikipedia.org/wiki/ISO_3166-1Business Country
Example: ‘DE’ for Germany
b_departmentStringBusiness Department
b_emailStringBusiness Email
b_job_titleStringJob Title
b_mobileStringBusiness Mobile
Example: +41 78 123 45 67
b_phoneStringBusiness Phone
Example: +41 78 123 45 67
b_reception_codeStringBusiness Reception code
b_regionStringBusiness Region
b_zipStringBusiness ZIP

9. Change Log #

9.1 v1.3.0 #

2024-07-25

  • Optimized subscribe flow
  • Optimized unsubscribe flow
  • Fixed button subscription state
  • Bundles are minimized

9.2 v1.2.0 #

2024-03-20

  • Removed Axios (http client) from bundle

9.3 v1.1.0 #

2020-02-10

  • General optimizations

9.4 v1.0.0 #

2018-01-15

  • Initial release