Home

Awesome

IBM Cloud Push Notifications Web SDK

Codacy Badge

The IBM Cloud Push Notifications service provides a unified push service to send real-time notifications to mobile and web applications. The SDK enables Safari, Chrome, Firefox browsers and Chrome Apps & Extensions to receive push notifications sent from the service.

Ensure that you go through IBM Cloud Push Notifications service documentation before you start.

Contents

Prerequisites

Installation

Download the Push Notifications Web Client SDK package.

Initialize SDK

Complete the following steps to initialize the SDK.

Initialize client Push SDK

Complete the following steps:

Ensure that you go through service configuration before starting the client configuration.

Choose any of the following options, based on your browser:

Initialize web Push SDK

Initialise the web push SDK with IBM Cloud Push Notifications service app GUID,clientSecret,websitePushIDSafari,deviceId and app Region.
If you are using dedicated service, use overrideServerHost and add any of the IBM Cloud region value.

var bmsPush = new BMSPush()
function callback(response) {
	alert(response.response)
}
var initParams = {
    "appGUID":"push app GUID",
    "appRegion":"Region where service hosted",
    "clientSecret":"push app client secret", 
    "websitePushIDSafari": "website Push ID for safari" // Optional parameter for Safari web push,
    "deviceId":"Optional deviceId for device registration" // optional parameter.
    "applicationServerKey":"VAPID key" // Get this value from swagger , under appliactions -> webpushServerKey,
    "overrideServerHost": "YOUR_SERVICE_HOST" // optional parameter
    
}
bmsPush.initialize(params, callback)

The App Region specifies the location where the Push service is hosted. You can use one of the following three values:

applicationServerKey is the VAPID implementaion for Chrome. This is required for new versions of Chrome. Get this value from the Push service swagger, under the applications -> webpushServerKey section.

Note:For debugging, use bmsPush.isDebugEnable(true).

Register for notifications

Use the register() or registerWithUserId() API to register the device with IBM Cloud Push Notifications service. Choose either of the following options:

WithUserId is the user identifier value with which you want to register devices in the push service instance.

Note: If userId is provided, the client secret value must be provided.

Unregistering from notifications

Push Notifications service tags

Retrieve available tags

To retrieve all the available tags, use the retrieveAvailableTags() method.

 bmsPush.retrieveAvailableTags(function(response) { //Retrieve available tags
   var jsonResponse = JSON.parse(response.response)
   var tagsArray = []
   for (i in jsonResponse.tags){
     tagsArray.push(jsonResponse.tags[i].name)
   }
   alert(tagsArray)
 })

Subscribe to tags

The subscribe() API will subscribe the web application for a list of given tags. After the device is subscribed to a particular tag, the device can receive push notifications that are targeted to an audience who have subscribed to that tag.

Use the following code snippet in your application to subscribe a list of tags.

bmsPush.subscribe(tagsArray,function(response) {
  alert(response.response)
})

Retrieve subscribed tags

The retrieveSubscriptions API will return the list of tags to which a website is subscribed.

Use the following code snippet in your application to get a subscription list.

bmsPush.retrieveSubscriptions(function(response){
   alert(response.response);
})

Unsubscribing from tags

To unsubscribe to a tag or tags, use the unSubscribe() method. Pass the array of tag names as parameter.

bmsPush.unSubscribe(tagsArray,function(response) {
	alert(response.response)
}

Parameterize Push Notifications

Add the variables in the Push initialize params values.

var templateValues = {
  "userName":"testname",
  "accountNumber":"3564758697057869"
}

var initParams = {
  "appGUID":"push app GUID",
  "appRegion":"Region where service hosted",
  "clientSecret":"push app client secret",
  "pushVaribales":templateValues
}

bmsPush.initialize(initParams, callback)

While registering the device IBM Cloud Push Notifications Web SDK will pass these variables to IBM Cloud Push Notifications service.

While sending push notification add the varibale key in {{}}


  {
      "message": {
          "alert": "hello {{username}} , balance on your account {{accountNumber}} is $1200"
      }
  }

Samples and videos

Learning more

Connect with IBM Cloud

Twitter | YouTube | Blog | Facebook |

======================= Copyright 2020-21 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.