React UI Kit Sample App
Reference implementation of React UI Kit, FCM and Push Notification Setup.
What this guide covers
- Prerequisite: the shared dashboard + Firebase web config/VAPID setup in the Getting Started guide.
- Service worker + Firebase Messaging wiring for foreground/background pushes.
- Token registration/unregistration with
CometChatNotificationsand navigation on notification click. - Testing and troubleshooting tips for web push.
How CometChat + FCM work on web
- Provider ID: Tells CometChat which Firebase credentials to use when sending to your web app.
- Tokens:
getTokenreturns a browser token (per origin/device). Register it after login:CometChatNotifications.registerPushToken(token, CometChatNotifications.PushPlatforms.FCM_WEB, providerId). - Handlers: Foreground messages come via
messaging.onMessage; background usesfirebase-messaging-sw.jswithonBackgroundMessage. - Navigation: Service worker sends a postMessage or focuses the client; the app routes to the right view.
Complete the Getting Started guide first — enable Push Notifications, add your FCM provider, and set up your Firebase web app config and VAPID key. This guide covers only the web app wiring.
1. Install dependencies
Install firebase SDK:2. Constants
File:src/AppConstants.js (or equivalent)
Set the CometChat Constants from your dashboard and Firebase config + VAPID key.
3. Configure Firebase (frontend)
File:src/firebase.js (or equivalent)
This code:
- Initializes Firebase app and messaging.
- Requests notification permission, fetches FCM token, and registers it with CometChat after login.
- Sets up foreground message handler to show notifications.
getToken calls.
4. Service worker (background pushes)
File:public/firebase-messaging-sw.js
This code:
- Initializes Firebase app in the service worker.
- Handles background messages with
onBackgroundMessage. - Manages notification clicks to focus the app and send data for navigation.
- Ensure your app registers the service worker (e.g., in
index.tsx) and listens formessageevents to navigate.
index.tsx) and listens for message events to navigate.
5. Request permission + register token after login
In your app initialization (e.g.,App.tsx):
This code:
- Requests notification permission.
- Fetches FCM token and registers it with CometChat after user login.
- Handles token refresh by re-registering if it changes.
- Run after the user logs in; retry on failure.
- On logout:
CometChatNotifications.unregisterPushToken()before ending the session. - Handle token refresh by calling
getTokenagain and re-registering if it changes.
- Initializes CometChat UI Kit.
- Initializes Firebase messaging.
- Logs in the user if not already logged in.
- Mounts the React app.
- Registers the push token after login.
6. Foreground + background handling
- Foreground:
messaging.onMessage→ show aNotificationor in-app toast; deep link using payload data. - Background/killed: service worker
onBackgroundMessageshows the notification;notificationclickfocuses the tab and sends a message for navigation. - Suppress duplicates if the conversation is already active.
7. Testing checklist
- Service worker registered (DevTools → Application → Service Workers shows “activated”).
- Permission prompt appears and is granted (
Notification.permission === "granted"). - Login → token fetched →
registerPushTokensucceeds (check console/logs). - Foreground message shows a notification; click navigates to the right chat.
- Background/tab inactive message shows a notification; click focuses tab and routes correctly.
- Logout →
unregisterPushTokenruns without errors.