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.
- Prerequisites
- Installation
- Initialize SDK
- Register for notifications
- Push Notifications service tags
- Parameterize Push Notifications
- Samples and videos
- Firefox 49.0 or later
- Chrome 54.0 or later
- Safari 9.1 or later
Download the Push Notifications Web Client SDK package.
Complete the following steps to initialize the 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:
-
Chrome and Firefox web browsers
Ensure that you go through service configuration before starting the client configuration. To install the Javascript SDK in Chrome and Firefox, complete the following steps:
-
Add the
BMSPushSDK.js,BMSPushServiceWorker.jsandmanifest_Website.jsonfiles to your project root folder. -
Edit the
manifest_Website.jsonfile.-
For Chrome browser:
{ "name": "YOUR_WEBSITE_NAME", "gcm_sender_id": "GCM_Sender_Id" }Change the
nameto your website's name and update thegcm_sender_idto your Firebase Cloud Messaging (FCM) [sender ID] (https://cloud.ibm.com/docs/services/mobilepush/push_step_1.html#push_step_1_android). Note that thegcm_sender_idvalue contains only numbers.Note:
gcm_sender_idis not required if you are using the applicationServerKey in BMS push init method. -
For Firefox browser, add the following values in
manifest_Website.jsonfile.{ "name": "YOUR_WEBSITE_NAME" }Change
nameto your website's name.
-
-
Change the
manifest_Website.jsonfile name tomanifest.json. -
Include the
manifest.jsonin<head>tag of your html file .<link rel="manifest" href="manifest.json">
-
Include IBM Cloud Web push SDK to the script ,
<script src="BMSPushSDK.js" async></script>
-
-
Safari web browsers
For Safari web browsers, add the
BMSPushSDK.jsin thehtmlfile:HTML <script src="BMSPushSDK.js" async></script> -
Chrome Apps
For installing the Javascript SDK in Chrome Apps, complete the following steps:
-
Add the
BMSPushSDK.jsandmanifest_Chrome_App.json. -
Configure the manifest file:
- For
Chrome Appin themanifest_Chrome_App.jsonfile providename,description, andicons. - Add the
BMSPushSDK.jsin theapp.background.scripts. - Change the
manifest_Chrome_App.jsontomanifest.json.
- For
-
In your
Javascriptfile (background.js), add the following to receive push notifications:chrome.gcm.onMessage.addListener(BMSPushBackground.onMessageReceived)chrome.notifications.onClicked.addListener(BMSPushBackground.notification_onClicked); chrome.notifications.onButtonClicked.addListener(BMSPushBackground.notifiation_buttonClicked);
-
-
Chrome Extensions
Add the
BMSPushSDK.jsandmanifest_Chrome_Ext.json.- Configure the manifest file:
- For
Chrome Extensionsin themanifest_Chrome_Ext.jsonfile, providename,description, andicons. - Add the
BMSPushSDK.jsin thebackground.scripts. - Change the
manifest_Chrome_Ext.jsontomanifest.json.
- For
- In your
Javascriptfile, add the following to receive notifications:
chrome.gcm.onMessage.addListener(BMSPushBackground.onMessageReceived) chrome.notifications.onClicked.addListener(BMSPushBackground.notification_onClicked); chrome.notifications.onButtonClicked.addListener(BMSPushBackground.notifiation_buttonClicked);
- Configure the manifest file:
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:
- For US Dallas -
us-south - For UK -
eu-gb - For Sydney -
au-syd - For Germany -
eu-de - For US East -
us-east - For Tokyo -
jp-tok
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).
Use the register() or registerWithUserId() API to register the device with IBM Cloud Push Notifications service. Choose either of the following options:
-
Register without UserId
To register without userId use the following pattern
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" } bmsPush.initialize(initParams, callback) bmsPush.register(function(response) { alert(response.response) })
-
Register with UserId
For
UserIdbased registration, use the following code snippet,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" } bmsPush.initialize(initParams, callback) bmsPush.registerWithUserId("your UserId",function(response) { alert(response.response) })
WithUserId is the user identifier value with which you want to register devices in the push service instance.
Note: If
userIdis provided, the client secret value must be provided.
-
To unregister from receiving notifications, add the following
unRegisterDevice()method:bmsPush.unRegisterDevice(function(response) { alert(response.response) }
-
To unregister the device from
UserIdbased registration, you have to call the registration method. See theRegister without userId optionin Register for notifications.
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)
})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)
})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);
})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)
}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"
}
}
-
For samples, visit - Github Sample
-
For video tutorials visit - IBM Cloud Push Notifications
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.