Push Notifications

Push notifications allows users to receive chat messages when they are offline.

The ChatCamp Android SDK receives push events from both ChatCamp backend and Firebase Cloud Messaging (FCM). The chat message is received received from ChatCamp protocol when the user is online i.e. if the Android app is open or working in background. Otherwise the chat message is pushed via FCM if the user is offline.

In order to enable your Android app to receive offline push notifications, follow these steps.

1. Retrieve FCM Server API Key and FCM Sender ID

  • Go to the Firebase Developer Console. You may either select an existing Firebase project or create a new one. Here we are going to create a new project. Click on Add project.
Firebase Developer ConsoleFirebase Developer Console

Firebase Developer Console

  • Enter your project name and click on Create Project.
Create a new Firebase ProjectCreate a new Firebase Project

Create a new Firebase Project

  • Next click on Add Firebase to your Android app button.
Add Firebase to Android AppAdd Firebase to Android App

Add Firebase to Android App

  • Enter your app package name along with app nickname and click on Register App button.
Register ChatCamp Android AppRegister ChatCamp Android App

Register ChatCamp Android App

  • Download the google-services.json file in your app root directory to add Firebase to your Android app.
Download google-services.json into Android app root directoryDownload google-services.json into Android app root directory

Download google-services.json into Android app root directory

  • Add Firebase SDK into your project by modifying build.gradle as instructed and click on Finish.
Modify build.gradle to include Firebase SDKModify build.gradle to include Firebase SDK

Modify build.gradle to include Firebase SDK

  • Next click on gear icon present next to project name and select Project Settings.
Open Firebase project settingsOpen Firebase project settings

Open Firebase project settings

  • Click on Cloud Messaging and jot down the Server Key and Sender ID. This will be later on used in the ChatCamp Dashboard.
Copy FCM Server Key and Sender IDCopy FCM Server Key and Sender ID

Copy FCM Server Key and Sender ID

2. Setup FCM in ChatCamp Dashboard

  • Navigate to the ChatCamp Dashboard and login with your credentials.
  • Click on the Settings section of the left hand navigation.
  • Finally add your FCM Server Key and Sender ID and click on Save.
Add FCM Server Key and Sender ID in ChatCamp DashboardAdd FCM Server Key and Sender ID in ChatCamp Dashboard

Add FCM Server Key and Sender ID in ChatCamp Dashboard

3. Add Firebase SDK to your Android App

Please follow this Firebase Cloud Messaging Android Client guide to install Firebase SDK into your Android app.

Once you do so, then you would be able to receive push notifications from ChatCamp backend via FCM.

4. Receiving push notifications

In order to receive push notifications, you first need to save the FCM token of user to ChatCamp backend. You may do so with the help of updateUserPushToken() function.

ChatCamp.updateUserPushToken(FirebaseInstanceId.getInstance().getToken(), new ChatCamp.UserPushTokenUpdateListener() {
  @Override
  public void onUpdated(User user, ChatCampException e) {
    // FCM Token successfully updated in the backend.
  }
});

The callback listener of ChatCamp.connect() function is an ideal place to call this function.

You will now start receiving FCM push notifications from ChatCamp.