- Chrome 50+
- Firefox 44+
- Edge 17+
- Opera 42+
I want to checkout the sample app
Push notifications sample app for Web (React)View on Github
Firebase Project Setup
Visit Firebase Console and login/signup using your Gmail ID.Step 1: Create a new Firebase Project

- You give a name to your project
- Add Google Analytics to your project (Optional)
- Configure Google Analytics account (Optional)
Step 2: Add Firebase to your Web App
- Click on the Web icon on the below screen and Register your app with a nickname.
- Once done, click on Continue to Console.

Step 3: Download the service account file

Extension settings
Step 1: Enable the extension
- Login to CometChat and select your app.
- Go to the Extensions section and Enable the Push Notifications extension.
- Open the settings for the extension and add all the mentioned settings and hit save.

Step 2: Save your settings
On the Settings page you need to enter the following:- Set extension version
- If you are setting it for the first time, Select
V2
to start using the token-based version of the Push Notification extension. - If you already have an app using
V1
and want to migrate your app to useV2
, then SelectV1 & V2
option. This ensures that the users viewing the older version of your app also receive Push Notifications. - Eventually, when all your users are on the latest version of your app, you can change this option to
V2
, thus turning offV1
(Topic-based) Push Notifications completely.
- Select the platforms that you want to support
- Select from Web, Android, Ionic, React Native, Flutter & iOS.
- Notification payload settings
- You can control if the notification key should be in the Payload or not. Learn more about the FCM Messages here.
- Push payload message options

- The maximum payload size supported by FCM and APNs for push notifications is approximately 4 KB. Due to the inclusion of CometChat’s message object, the payload size may exceed this limit, potentially leading to non-delivery of push notifications for certain messages. The options provided allow you to remove the sender’s metadata, receiver’s metadata, message metadata and trim the content of the text field.
- The message metadata includes the outputs of the Thumbnail Generation, Image Moderation, and Smart Replies extensions. You may want to retain this metadata if you need to customize the notification displayed to the end user based on these outputs.
- Notification Triggers

-
Select the triggers for sending Push Notifications. These triggers can be classified into 3 main categories:
- Message Notifications
- Call Notifications
- Group Notifications
- These are pretty self-explanatory and you can toggle them as per your requirement.
Web App Setup
Step 1: Folder and files setup
Create a folder with the following three files:Files | Description |
---|---|
index.html | Displays a simple User Login Form. |
PushNotification.js | File with the logic to initialize CometChat and Firebase. |
firebase-messaging-sw.js | Service worker shows Push Notifications when the tab is either in the background or closed. |
Step 2: Add the Firebase Config to the HTML File
- Go to the Firebase Console and click on the Web app and open up the Settings page.
- Go to the “General” tab on the Settings page.
- Scroll down and copy the Firebase SDK snippet and paste in the <head> tag of your index.html file.

Step 3: Setup index.html file
- Include the latest CometChat library using CDN.
- Register the service worker file.
-
Also, include the
PushNotification.js
. -
The <body> has a simple form:
- Text input for UID.
- Login button.
- Logout button.
index.html
file should look like this:
Step 4: Setup the service worker file
- Use
importScripts
to include thefirebase-app.js
andfirebase-messaging.js
files in the service worker. - Also paste in the
FIREBASE_CONFIG
object again in this file. - Initialize the Firebase object using the config.
- Call the messaging() on the Firebase object.
firebase-messaging-sw.js
file should look like this:
Step 5: Setup the PushNotification.js file
Now our simple web app has the following:- Setup required to start using Firebase SDK.
- Service worker registration when the index.html loads for the first time.
- Initialize CometChat.
- Login using CometChat user.
- Ask for the User’s permission to show Push Notifications.
- If permission is granted, obtain the
FCM_TOKEN
. - Register the obtained
FCM_TOKEN
with the extension.
- First delete the token using the firebase object.
- Logout CometChat user.
login
and logout
functions in the PushNotifications.js
file. You can copy paste the below code. Do not forget to replace the APP_ID
, REGION
, AUTH_KEY
of your app in the code below.
Start receiving Push Notifications
- You can now host the project folder using Nginx, Apache web server, or even VSCode Live server extension.
- Launch the web app in a browser and open the browser console to see the logs.
- Enter the UID of the user and click on login.
- When asked for permission to show notifications, click on Allow.
- Once you see logs saying that the FCM Token has been registered, either send the browser tab to the background or close it completely.
- Send a message to this logged-in user from another device (using our Sample Apps) and you should be able to see the Push Notifications.
Stop receiving Push Notifications
- Reopen the previous closed browser tab and click on logout.
- The
FCM_TOKEN
will be deleted on the extension’s end on theCometChat.logout()
call. - As a good practice, the
FCM_TOKEN
should also be deleted using thefirebase.messaging().deleteToken()
.
Custom body for notifications
To send custom body for notifications or to receive notification ofCustomMessage
, you need to set metadata while sending the CustomMessage
.