To configure Firebase Push Notifications for CometChat create a firebase project at https://console.firebase.google.com/ and follow the steps below:
1.Click on “Add Firebase to your Android app” or “Add another app” option.
- Add the android package name, the application nickname and the SHA-1 details. Click on the Register App button once the details are entered. Click on ‘Download google-services.json’ button to get the google-services.json file’. Add the file to the root directory of your project. Add the dependencies as mentioned to the gradle files.
- Go to Android app settings by clicking on “settings” option in more settings menu(Vertical dotted icon)
- Get the “Web API Key” to configure firebase push notification service from CometChat Admin Panel.
- Add the “Legacy Server Key” which can be found in the Cloud Messaging section as “Firebase server key” in CometChat Admin Panel under Settings -> Mobile tab
- Refer https://firebase.google.com/docs/cloud-messaging/android/client to configure your mobile app to receive the push notifications.
You need to subscribe to a channel from where you will get the push notification. You will get this channel from the response received in gotProfileInfo() callback when you call the subscribe method using the SDK.The response of callbacks contains a key named as “push_channel”. This contains the push notification channel. You can subscribe to push channel using the following line:
For push notifications in the group, you will get “push_channel” from the response received in success callback of the joinGroup method. Once you subscribe to this channel, you will start receiving push notifications for the group.
For push notifications in Announcement, you will get “push_an_channel” in the response of gotProfileInfo() callback when you call the subscribe method using the SDK. In the response, you will receive a key named ‘push_an_channel’. Once you subscribe to this channel you will start receiving the push notification for announcements sent from CometChat administration panel.
Note: You will receive the push notification in the form of the data payload in the service class extending FirebaseMessagingService. For SDK versions below 7.0.10, you will require processing this data and show notification and for the versions, we are providing Helper method in which you can pass the RemoteMessage object received in onMessageReceived as following: