Configuring SDK push notifications

Configuring push notifications in FCM

Sending a chat message to the user who’s currently offline in your mobile app will result in them receiving a push notification, like the one from regular messengers.

1. Turn on Google services for your app

In order for push notifications to work we are using Firebase Cloud Messaging (FCM). If you already have a Firebase-project with notifications on, skip this step. Choose a name for your project and click Create project:

Select Cloud Messaging after you are done with project settings:

Select the Android icon:

Set your package name and click Register App:

2. Configure the app to receive push notifications

Click Download google-services.json, to load the configuration file. Put it into your app level.

Click Next and add dependency to the file of build.gradle on the app level:

dependencies {
implementation 'com.google.firebase:firebase-messaging:17.+'
}

Don’t forget to apply a plugin at the end of the same file:

apply plugin: 'com.google.gms.google-services'

Click Next.

3. Add your Service Account Key to Dashly settings

Select your project in the Firebase console, go to Project settings, open Service accounts tab. Click Generate New private key. It will prompt a JSON-file with the key to download.

You can manage the keys in Google Cloud Platform settings. To do so, open the Firebase console, go to the Service accounts tab and click the Manage service account permissions link. While in Dashly, go to the Settings → Developers section and upload your key.

4. Set an FCM thumbnail (optional)

A thumbnail can be set up using the following method:

Carrot.setNotificationIcon(notificationIconId)

where notificationIconId is the thumbnail source ID.

5. Configuring the app that already uses FCM

This step may be of importance for the apps that are currently using FCM to work with their own push notifications. Here is how to configure your FirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived (RemoteMessage remoteMessage) {
        Map<String, String> data = remoteMessage.getData();
        if (data.containsKey(NotificationsConstants.CQ_SDK_PUSH) && "true".equals(data.get(NotificationsConstants.CQ_SDK_PUSH))) {
            Carrot.sendFirebaseNotification(remoteMessage);
        } else {
            //DO HOST LOGIC HERE
        }
    }
}

Quick tip

If you are having issues with the FCM configuration, check the following:

  • Check if you have enabled the Send a push notification option for manual messages

  • Check if push notifications for the app are allowed on your test device: Settings → Notifications and status bar → Notification manager (path may differ depending on the Android version).

  • Check Push Server API key.

  • Check if google-services.json is located in the correct directory.