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.
- Enter your project name and click on Create Project.
- Next click on Add Firebase to your Android app button.
- Enter your app package name along with app nickname and click on Register App button.
- Download the google-services.json file in your app root directory to add Firebase to your Android app.
- Add Firebase SDK into your project by modifying
build.gradle
as instructed and click on Finish.
- Next click on gear icon present next to project name and select Project Settings.
- Click on Cloud Messaging and jot down the
Server Key
andSender ID
. This will be later on used in the ChatCamp Dashboard.
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
andSender ID
and click on Save.
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.
Updated less than a minute ago