Skip to main content
Migrate to Token-based Push NotificationsYou can check out our new Token-based implementation here.
The Push Notification extension allows you to send push notifications to mobile apps and desktop browsers. Push notifications will work in all desktop browsers which support Push API. These include:
  1. Chrome 50+
  2. Firefox 44+
  3. Edge 17+
  4. Opera 42+

Step 1: Add Firebase to your app

To configure Firebase Push Notifications for your apps create a Firebase project at Firebase Console. If you have previously not created a Firebase project for your app Click Add project. If you already have created a project for your app in which you wish to integrate CometChat, select the same project and download the config file.

Step 2 : Obtain Firebase configuration for your platform

For Web

  1. Sign into Firebase and open your project.
  2. On the Overview page, click Add app.
  3. Select Add Firebase to your web app.
  4. Copy the snippet and add it to your HTML application.

For Android

  1. Sign into Firebase and open your project.
  2. On the Overview page, click Add app.
  3. Select Add Firebase to your Android app.
  4. Follow the on-screen instructions and finally download the google-services.json file.

For iOS

  1. Sign into Firebase and open your project.
  2. On the Overview page, click Add app.
  3. Select Add Firebase to your iOS app.
  4. Enter the relevant details like your bundle ID and download the GoogleService-Info.plist file.
  5. Move this plist file to the root of your XCode project. If prompted, select to add the config file to all targets as follows:

For React Native

  1. For React Native to Android, you need to download the google-services.json file.
  2. For React native to iOS, you need to download the GoogleServices-Info.plist file.

For Capacitor, Cordova & Ionic

  1. For React Native to Android, you need to download the google-services.json file.
  2. For React native to iOS, you need to download the GoogleServices-Info.plist file.
  3. For web, you will need the Firebase Config object.

Step 3: Extension setup

  1. You need the Firebase Service key which you can get from Firebase console.
    1. Open your Firebase app
    2. Click on the Settings Cog in the left navigation menu
    3. Select Project Settings and go to the Cloud Messaging tab
    4. Add a Server Key and copy it for further use.
  2. Login to the CometChat Dashboard and select your app.
  3. On the Extensions page, enable the Push Notifications extension.
  4. Open the Settings for this extension.
  5. Enter FCM Server key.
  6. Select the platforms of choice.
  7. Enter the title for notifications.
  8. You can also toggle the triggers for sending Push Notifications. The triggers can be classified into 3 main categories:
    1. Message Notifications
    2. Call Notifications
    3. Group Notifications

Step 4: Application Setup

In order to use the topic-based Push Notifications, you need to subscribe to topics. In case of CometChat, you need to subscribe to 2 different types of topics:
  1. Topic to receive Push Notifications for one-on-one messages and calls.
  2. Topic to receive Push Notifications for group messages and calls.
Also, you can:
  1. Subscribe to one topic
  2. Subscribe to all the topics
Below steps guide you with this process of subscription to topics and specific setup for the platform of your choice.

For Web

1. Installing Firebase SDK

2. Initialize Firebase app

  1. Create a index.html file and initialize the Firebase app. This has to be done only once.
  2. Here, you will need the Firebase Configuration object that you copied from the Firebase Console.
  3. Also, register the service worker.

3. Request permission for Web Push

  1. Create PushNotification.js file.
  2. Insert the following code so that the browser asks for permission to show Push Notifications.
  3. If the permission is granted, you will receive a FCM registration token.

4. Subscribe to topic(s)

  1. In the success callback of CometChat.login(), you can start the subscription process.
  2. As mentioned earlier, you can either subscribe to one topic or subscribe to all.
  3. The format for the name of user topic is AppID_user_UID
  4. The format for the name of a group topic is AppID_group_GUID

5. Receive Messages

Create a firebase-messaging-sw.js file which will handle showing notifications

6. Unsubscribe from topics

Before you logout the user using CometChat.logout() method, you can unsubscribe from topics to stop receiving Push notifications for a logged out user.

7. Handle Custom messages

To receive notification of CustomMessage, you need to set metadata while sending the CustomMessage.

For Android

1. Setup client app

To enable Firebase products in your app, add the google-services plugin to your Gradle files. In your root-level (project-level) Gradle file (build.gradle), Check that you have Google’s Maven repository. In your module (app-level) Gradle file (usually app/build.gradle), apply the Google Services Gradle plugin and add the dependencies for the Firebase Cloud Messaging.

2. Using FirebaseMessaging Service & Broadcast Receiver

FirebaseMessagingService provides a method which helps to register token for client app by overriding onNewToken.

3. Subscribe and unsubscribe to topic(s)

  1. After successful login using CometChat.login(), you can start the subscription process.
  2. As mentioned earlier, you can either subscribe to one topic or subscribe to all.
  3. The format for the name of user topic is AppID_user_UID
  4. The format for the name of a group topic is AppID_group_GUID
  5. You can unsubscribe when the user logs out (i.e. before calling the CometChat.logout() method)

4. Receive Messages

To receive messages, you need to override the onMessageReceived (RemoteMessage remoteMessage).
CallNotificationAction.java is a Broadcast Receiver which is used to handle call events when app is in the background. Since Android O, there have been certain restrictions added for background tasks and users cannot launch intent directly from the service. More details can be found here.
You also need to add both of the above mentioned file in your AndroidManifest.xml to make Push notification work in Background as well.
6. Notification Channel From Android O and above you need to use NotificationChannel to show notifications. You can add the below method in your Application class and call it in OnCreate(). This method will manage the notification channel for your app.

For iOS

Apple Developer Portal

The following steps in this section are written on the assumption that you already have an app ID assigned to your client app 1. Create a Certificate Signing Request To obtain a signing certificate required to sign apps for installation on iOS devices, you should first create a certificate signing request (CSR) file through Keychain Access on your Mac.
  1. Open the Keychain Access from the utility folder, go to Keychain Access > Certificate Assistant > Request a Certificate From a Certificate Authority, and then click
  2. The Certificate Information dialog box appears. Enter the email address that you use in your Apple Developer account, and enter a common name for your private key. Don’t enter CA email address, choose Saved to disk, and then click the Continue button.
  3. Specify the name of your CSR to save and choose the location to save the file on your local disk. Then your CSR file is created, which contains a public/private key pair.
2. Create an SSL Certificate
  1. Sign in to your account at the Apple Developer Member Center.
  2. Go to Certificates, Identifiers & Profiles. In the Identifiers > App IDs and select the Push Notifications service under Application Services
  3. Click the Edit button.
  4. Under the Push Notifications service, choose which SSL certificate to create either Development or Production.
  5. In the Generate your certificate pane that appears after the selection, under Upload CSR file., upload the CSR file you created through the Choose File… button. To complete the process, choose Continue. When the certificate is ready, choose Download to save it to your Mac.
  6. In order to install the downloaded certificate to the KeyChain Access on your Mac, double-click it. You can find the certificate in the KeyChain Access > login > Certificates.
3. Export and update .p12 file to Firebase
  1. Type a name for the .p12 file and save it to your Mac.
  2. Browse to the location where you saved your key, select it, and click Open. Add the key ID for the key (available in Certificates, Identifiers & Profiles in the Apple Developer Member Center) and export it.
4. Upload your APNs Certificates
  1. Go to Firebase console and open your project.
  2. Inside your iOS project in the Firebase console, select settings and then select the Cloud Messaging tab.
  3. Scroll down to iOS app configuration, click the Upload button for APNS certificate.
  4. Browse to the location where you saved your APNs Certificates, select it, and click Open.

iOS App Setup

1. FCM Client on iOS Add the Firebase SDK, Add the firebase pods that you want to install. You can include a Pod in your Podfile like this:
Import the Firebase module in your AppDelegate:
Configure a FirebaseApp shared instance, typically in your application’s application:didFinishLaunchingWithOptions
Register for remote notification, typically in your application’s `application:didFinishLaunchingWithOptions```
The FCM Registration token
2. Subscribe and Unsubscribe to topic(s)
  1. The format for the name of user topic is AppID_user_UID_ios
  2. The format for the name of a group topic is AppID_group_GUID_ios
3. Receive Remote notifications This typically happens in your application’s AppDelegate
Receive and display notifications for CustomMessage, you need to set the metadata while sending the CustomMessage:

For React Native

1. Installing Firebase SDK

Install the react-native-firebase package in your project.
2. Android Setup To allow the Android app to securely connect to your Firebase project, a configuration file must be downloaded and added to your project. Download the google-services.json file and place it inside of your project at the following location: /android/app/google-services.json. Configure Firebase in Android: To allow Firebase on Android to use the credentials, the google-services plugin must be enabled on the project. This requires modification to two files in the Android directory. Add the google-services plugin as a dependency inside of your /android/build.gradle. Execute the plugin by adding the following to your /android/app/build.gradle file.

3. iOS Setup

To allow the iOS app to securely connect to your Firebase project, a configuration file must be downloaded and added to your project. On the Firebase console, add a new iOS application and enter your project details. The “iOS bundle ID” must match your local project bundle ID. The bundle ID can be found within the “General” tab when opening the project with Xcode. Download the GoogleService-Info.plist file. Using Xcode, open the projects /ios/{projectName}.xcodeproj file (or /ios/{projectName}.xcworkspace if using Pods). Right-click on the project name and “Add files” to the project, as demonstrated below:
Select the downloaded GoogleService-Info.plist file from your computer, and ensure the “Copy items if needed” checkbox is enabled.
Configure Firebase in iOS: To allow Firebase on iOS to use the credentials, the Firebase iOS SDK must be configured during the bootstrap phase of your application. To do this, open your /ios/{projectName}/AppDelegate.m file, and add the following: At the top of the file, import the Firebase SDK:
Within your existing didFinishLaunchingWithOptions method, add the following to the top of the method:
In the Firebase console, you have to include either APNs Authentication Key or APNs Certificate in Project Settings > Cloud Messaging in order to receive push notifications. Turn on the following two capabilities in Xcode: a. Push Notifications & b. Background Modes - Check only Remote Notifications.
Lastly, Open your projects /ios/Podfile and add any of the globals shown below to the top of the file:
4. Handling Push Notifications You can refer to the below code for handling Push Notifications in React Native.

5. Subscribe to and u nsubscribe from topics

Refer to the JavaScript section above for subscription and unsubscription code.

For Capacitor, Cordova & Ionic

1. Firebase Plugins For Cordova & Ionic, there are numerous plugins available via NPM which can be used to set up push notifications for your apps like FCM Plugin and Push Plugin. To setup Push Notification, you need to follow the steps mentioned in the Plugin’s Documentation. You need to make different apps on the firebase console for each platform respectively (Android, iOS). 2. Subscribe and unsubscribe process Refer to the JavaScript section above for subscription and unsubscription code. 3. Receiving Push notifications Here you can use the callback provided by the plugin. For eg: If you are using the FCM Plugin you can receive the messages as follows:
This should ideally be added in the app.component.ts file and should be called in the success of the platform.ready(). For other plugins, you can refer to the documentation provided by the plugin to check how messages can be received using that plugin. Once you have started receiving messages, you can act on the received messages accordingly as per your requirements.

Converting Push payload to message object

CometChat SDK provides a method CometChat.CometChatHelper.processMessage() to convert the message JSON to the corresponding object of TextMessage, MediaMessage, CustomMessage, Action or Call. This code needs to be added to the onMessageReceived() method of the FirebaseMessagingService class.
Attachments can be of the following types:CometChatConstants.MESSAGE_TYPE_IMAGE
CometChatConstants.MESSAGE_TYPE_VIDEO
CometChatConstants.MESSAGE_TYPE_AUDIO
CometChatConstants.MESSAGE_TYPE_FILE