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.
2106

Firebase Developer Console

  • Enter your project name and click on Create Project.
1772

Create a new Firebase Project

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

Add Firebase to Android App

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

Register ChatCamp Android App

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

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.
1430

Modify build.gradle to include Firebase SDK

  • Next click on gear icon present next to project name and select Project Settings.
2108

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.
1988

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.
2104

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.