Skip to main content
Learn how to set up Push notifications for React Native using Firebase Cloud Messaging or FCM.

I want to checkout the sample app

React Native Push notifications sample appView on Github

Firebase Project Setup

Visit Firebase and login/signup using your Gmail ID.

Step 1: Create a new Firebase Project

Head over to the Firebase Console to create a new project.
This is a simple 3 step process where:
  1. You give a name to your project
  2. Add Google Analytics to your project (Optional)
  3. Configure Google Analytics account (Optional)
Click on Create and you are ready to go.

Step 2: Add Firebase to your App

React native setup will require 2 files for Android and iOS:
  1. For Android, you need to download the google-services.json file from the Firebase console.
  2. For iOS, you need to download the GoogleService-Info.plist file from the Firebase console.

Step 3: Download the service account file

Extension settings

Step 1: Enable the extension

  1. Login to CometChat and select your app.
  2. Go to the Extensions section and Enable the Push Notifications extension.
  3. Open up the settings and save the following settings.

Step 2: Save your settings

On the Settings page you need to enter the following:
  1. 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 use V2, then Select V1 & 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 off V1 (Topic-based) Push Notifications completely.
  1. Select the platforms that you want to support
  • Select from Web, Android, Ionic, React Native, Flutter & iOS.
  1. Notification payload settings
  • You can control if the notification key should be in the Payload or not. Learn more about the FCM Messages here.
  1. 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.
  1. Notification Triggers
  • Select the triggers for sending Push Notifications. These triggers can be classified into 3 main categories:
    1. Message Notifications
    2. Call Notifications
    3. Group Notifications
  • These are pretty self-explanatory and you can toggle them as per your requirement.

App Setup

Step 1: Initial plugin setup

  1. For React Native, there are numerous plugins available via NPM which can be used to set up push notifications for your apps. react-native-firebase and react-native-notifications are just the two out of many available.
  2. To setup Push Notification, you need to follow the steps mentioned in the Plugin’s Documentation.
At this point, you will have:
  1. Two separate apps created on the Firebase console. (For Android and iOS).
  2. Plugin setup completed as per the respective documentation and our reference.

Step 2: Register FCM Token

  1. This step assumes that you already have a React Native app setup with CometChat installed. Make sure that the CometChat object is initialized and user has been logged in.
  2. On the success callback of user login, you can fetch the FCM Token and register it with the extension as shown below:
  1. Registration also needs to happen in case of token refresh as shown below:
For React Native Firebase reference, visit the link below:

Step 3: Receive Notifications

We send Data Notifications and you need to handle displaying notifications at your end. For eg: Using Notifee

Step 4: Stop receiving Notifications

  1. Simply logout the CometChat user and you will stop receiving notifications.
  2. As a good practice, you can also delete the FCM Token by calling deleteToken on the messaging object.

Advanced

Handle Custom Messages

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

Converting push notification 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.
Type of Attachment can be of the following the type
1.CometChatConstants.MESSAGE_TYPE_IMAGE
2.CometChatConstants.MESSAGE_TYPE_VIDEO
3.CometChatConstants.MESSAGE_TYPE_AUDIO
4.CometChatConstants.MESSAGE_TYPE_FILE
Push Notification: Payload Sample for Text Message and Attachment/Media Message

Integrating ConnectionService and CallKit Using CometChat Push Notification

  • Currently we can only handle default calling notification
  • Whenever the user answers the call we use RNCallKeep.backToForeground(); method to bring the app in to foreground but in some devices you might need to add few more permissions for this to work For example, In MIUI 11 you need to permission for Display pop-up windows while running in the background
  • When the iOS app is in lock state we are not able to open the app so the call start on callkeep it self and you can hear the audio but if you want a video call then the user has to unlock the phone click on the app icon on call screen.
  • If you want to use the callkit and connection service in foreground then you might consider turning the callNotifications settings in UI kit settings. For more information in UI kit settings check the documentation.

Setup push notification

  • Android
Kindly follow the instruction for setting Firebase Cloud Messaging explained here
  • iOS
For iOS we use Apple Push Notification service or APNs to send push notification and VOIP notification. To configure this we need to follow some additional steps

Step 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.
  1. 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. <img align=“center” src=”./images/step1.2.png”> <br></br>
  2. 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.

Step 2: Create an SSL certificate

  1. Sign in to your account at the Apple Developer Member Center.
  2. Go to Certificates, Identifiers & Profiles.
  1. Create new Certificate by clicking on the + icon.
  1. Under Services, select - Apple Push Notification services SSL (Sandbox & Production)
  1. Select your App ID from the dropdown.
  1. 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.

Step 3: Export and update .p8 certificate

  1. To generate a .p8 key file, go to Apple developer account page, then select Certificates, IDs & Profiles.
  2. Select Keys and click on the ”+” button to add a new key.
  3. In the new key page, type in your key name and check the Apple Push Notification service (APNs) box, then click “Continue” and click “Register”.
  4. Then proceed to download the key file by clicking Download.
  5. Make note of the Key ID, Team ID and your Bundle ID for saving in the Extension’s settings.
If you wish to use the .p12 certificate instead, do the following:
  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.
  3. DO NOT provide an export password when prompted.
  4. The .p12 file will be required in the next step for uploading in the CometChat Dashboard.

Extension settings

Step 1: Enable the extension

  1. Login to CometChat and select your app.
  2. Go to the Extensions section and Enable the Push Notifications extension.
  3. Open the settings for this extension and save the following.

Step 2: Save your settings

On the Settings page you need to enter the following:
  1. Set extension version The extension version has to be set to ‘V2’ or ‘V1 & V2’ in order to use APNs as the provider.
  2. Select Platforms You can select the platforms on which you wish to receive Push Notifications.
  3. Firebase Cloud Messaging Settings This includes the FCM Server key that you can fetch from the Firebase Dashboard.
  4. APNs Settings You can turn off the Production mode when you create a development build of your application. Upload the .p12 certificate exported in the previous step.
  5. Push Notifications Title This is usually the name of your app.
  6. Notification Triggers Select the triggers for sending Push Notifications. These triggers can be classified into 3 main categories:
    1. Message Notifications
    2. Call Notifications
    3. Group Notifications
    These are pretty self-explanatory and you can toggle them as per your requirement.

Installation

We need to add two packages for this
  • React-native-CallKeep
This package also require some additional installation steps. Follow this link to install react-native-callkeep
  • React Native VoIP Push Notification
This package also require some additional installation steps. Follow this link to install react-native-voip-push-notification.

App Setup

First you need to Setup CallKeep at the start of the app in Index.js
In order to handle connectionService and CallKit we have made a helper call.

Android

In android we are going to use Firebase push notification to display Call notification So basically when ever we receive a push notification for call we display call notification. we need to add a listener to listen to notification when the app is background or foreground state.

iOS

In iOS we use APNs push and voip push notification to display push notification and display call CallKit for calls. The notification are handled in Native iOS You need to add the code in AppDelegate.m file to display CallKit