Creating and sending push notifications to Chrome web by using Event Notifications
Create an Event Notifications service, add a push destination for Chrome Push Notifications, and send messages to Chrome web devices.
What is Event Notifications?
Event Notifications is an event notification routing service that informs you about critical events that occur in your IBM Cloud account or triggers automated actions by using webhooks. You can filter and route event notifications from IBM Cloud services like Availability Monitoring, to email, SMS, push notifications, and webhooks.
How do clients use Chrome web Push Notifications?
The following diagram shows you how clients use Chrome web Push Notifications.
Objectives
This tutorial shows you how to send push notifications as follows:
- Create a website with Event Notifications.
- Get Chrome web credentials.
- Download the SDK and complete the notifications setup.
- Configure and send Chrome web Push Notifications to a browser.
Before you begin
You must have the following prerequisites in place:
- Download and install any code editor to correct website code by using HTML, CSS, and js.
- A Google account to log in to Firebase console to get your Sender ID and Server API Key.
- An IBM Cloud account. If you do not have one, create an IBM Cloud account.
Create an Event Notifications service instance
- Log in to your IBM Cloud account.
- In the IBM Cloud catalog, search
Event Notifications > Event Notifications
. - Select a
Region
from the list of supported regions and select apricing plan
. - Provide a
Service name
. - Select a
resource group
. - Accept the licensing agreements and terms by clicking the checkbox.
- Click
Create
.
Get FCM credentials
Firebase Cloud Messaging (FCM) is the gateway that delivers push notifications to Chrome web devices. To set up the Chrome web Push destination on the console, you must get your FCM credentials (Server key). The server key is stored securely and used by the Event Notifications service to connect to the FCM server.
- Go to the Firebase Console. A Google user account is required.
- Click
Create a project
. If you are already having a project, then clickAdd Project
. - In the
Create a project window
, enter a project name, and accept the terms and enable or disable Google analytics (optional) by selecting the toggle switch and clickContinue
. - If Google analytics is enabled, then in the
Configure Google Analytics
window, choose theAnalytics location
, and accept the terms. - Click
Create Project
. - Click
Continue
when the new project is ready. - In the navigation panel, select the
settings
icon next to theProject Overview
and selectSettings > Project settings
. - Click the
Cloud Messaging
tab to view your project credentials:Server Key
.
Add a generic API source
Take the following steps:
- Go to the
Sources
section of the Event Notifications dashboard. - Click
Add
and select an API Source. - Type a name and an optional description and click
Add
.
Create an Event Notifications destination
Click Destinations
in the Event Notifications console and add the following destination details:
Name
: add a name for the Destination.Description
: add an optional description for the destination.Type
: selectChrome Push Notifications
type from the dropdown list.- Select a destination plan: Pre-production destination or Production destination.
Pre-production destination
- select this destination as low-cost push destination, for your development and test environments.Production destination
- use the full capability of this destination. Unlimited devices and outbound messages allowed.
- Add your website URL to get it allow listed.
- Add the
Server Key
, which you got fromGet FCM credentials
section. - Click Add.
- When Chrome destination is created, edit the created destination to get public vapId key required for web sdk.
Create an Event Notifications topic
Select Topics
in the Event Notifications console and click Create
. Enter the following topic details:
Name
: enter a name for the topic.Description
: add an optional description for the topic.Source
: select a source from the dropdown list.Event type
: select event type from the dropdown list.Event sub type
select event sub type from the event sub type dropdown list.Severity
: select severity from the severity dropdown list.Advanced conditions
: write your own custom conditions, which must follow jsonpath specifications.
Create an Event Notifications subscription
Click Subscriptions
in the Event Notifications console. Enter the following subscription details:
Click
Create to display subscription wizard.- Complete the following subscription details:
Subscription name
: name of the subscription.Subscription description
: add an optional description.
- Under the
Subscribe to a topic
section, select a topic from the drop-down list and select a destination from the destination drop-down list. Destination type
: select type underDestination
and clickAdd
.
Set up Event Notifications Chrome web SDK
The Chrome web SDK enables Chrome websites to receive push notifications. Complete the following steps to install Event Notifications Chrome web SDK, initialize the SDK, and register for notifications for your website.
-
To include the SDK in your project, add the
ENPushSDK.js
,ENPushServiceWorker.js
, andmanifest_Website.json
files to your project root folder. -
Edit the
manifest_Website.json
file.{ "name": "YOUR_WEBSITE_NAME" }
-
Change the
manifest_Website.json
file name tomanifest.json
. -
Include the
manifest.json
in<head>
tag of your html file.<link rel="manifest" href="https://github.com/IBM/event-notifications-destination-webpush-sdk/blob/main/manifest_Website.json">
-
Include IBM Cloud web push SDK to the script.
<script src="https://github.com/IBM/event-notifications-destination-webpush-sdk/blob/main/ENPushSDK.js" async></script>
-
Complete the following steps to enable website to initialize the SDK.
var enPush = new ENPush() function callback(response) { alert(response.response) } var initParams = { "instanceGUID": "<instance_guid>", "apikey": "<instance_apikey>", "region": "<region>", "deviceId": "<YOUR_DEVICE_ID>", "chromeDestinationId": "<chrome_destination_id>", "chromeApplicationServerKey": "<Chrome_VapId_public_key>" } enPush.initialize(initParams, callback)
-
region
: Region of the Event Notifications instance. For example,us-south
,eu-gb
,au-syd
,eu-de
oreu-es
. -
deviceId
: OptionaldeviceId
for device registration.
-
-
To register for notifications, use the
register()
orregisterWithUserId()
API to register the device with Event Notifications service. Choose either of the following options:-
Register without
UserId
:enPush.register(function(response) { alert(response.response) })
-
Register with
userId
. ForuserId
based notification, the register method accepts one more parameter -userId
.bmsPush.registerWithUserId("UserId",function(response) { alert(response.response) })
UserId
is the user identifier value with which you want to register devices in the push service instance.
-
-
The
subscribe
API subscribes the device for a tag. After the device is subscribed to a particular tag, the device can receive notifications that are sent for that tag. Add the following code snippet to your web application to subscribe to a list of tags.enPush.subscribe(tagName, function(response) { alert(response.response) })
-
When the setup is complete, run your application and register for push notifications.
Send notifications to the Chrome web device
Use the Send notification API to send the push notification for the Chrome web device. You can use the Node or Go admin SDK instead of calling the API directly.
Received notification on the chrome website in MacOS.