Push Notifications Setup Guide: Android

Chatkit relies on Firebase Cloud Messaging (FCM) to deliver push notifications to Android application users on your behalf. When we deliver push notifications, we use your FCM credentials. This page guides you through the process of getting an FCM Server Key and how to provide it to Chatkit.

Head over to the Pusher dashboard and create a new instance of Chatkit. After that, you can start following the documentation steps.


Visual Walkthrough

To help you find your way around, this video mirrors the steps listed below.

Open Firebase Console

The first step is to go your Firebase console. Sign up if you haven't already.

Create a New App

If you are using a pre-existing Firebase app, you can jump ahead to Get Your FCM Server Key. Otherwise you will create one now.

To create a new app, you'll need to supply the name and region (0:03 ).

Then from your app's Overview page, click on the Android logo (0:13 ) and write your Android application's package name (0:18 ) to register your app in Firebase.

Download the Firebase Configuration File

Once the app is registered, you should click the button to download google-services.json (0:25 ).

Keep a note of the file's location, as eventually you will add it to your Android app's project on your computer.

Get Your FCM Server Key

Go to your Firebase project's settings page (0:38 ), and then go to the Cloud Messaging tab (0:46 ).
Now you can copy the FCM Server Key by clicking on the clipboard button (0:51 ).

This FCM Server Key can be saved to your Chatkit instance in the dashboard.

Add Firebase Config File to Your Project

Have you downloaded the google-services.json config file from your Firebase project console? If not, see this video.

Move Config File Into Project

Move your google-services.json config file into your Android Studio project, in the app directory:

YOUR_ANDROID_PROJECT/app/google-services.json

Update Your Project-Level Gradle Config

Add the Google Services classpath to the dependencies section of your project-level build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
 
buildscript {
 
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        // Add this line
        classpath 'com.google.gms:google-services:4.0.1'
 
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
 
allprojects {
    repositories {
        google()
        jcenter()
    }
}
 
task clean(type: Delete) {
    delete rootProject.buildDir
}

Update Your App-Level Gradle Config

In order to update your app-level build.gradle you will need to:

  • Add the Firebase Messaging SDK to your dependencies
  • Add the Google Services plugin to the end of the file
apply plugin: 'com.android.application'
 
android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.exampleapp"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
 
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    // Add line
    implementation 'com.google.firebase:firebase-messaging:17.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
 
// Add this line to the end of the file
apply plugin: 'com.google.gms.google-services'

Synchronize Gradle

Synchronize Gradle by pressing the "Sync Now" button:

Gradle 'Sync Now' button in Android Studio

Enable Push Notifications

Final step is to enable push notifications. This can be done once the connection with the Chatkit service was successfully established on the instance of the CurrentUser.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
chatManager.connect { result ->
  when(result) {
      is Result.Success -> {
          // We have connected!
          val currentUser = result.value // CurrentUser
          currentUser.enablePushNotifications { result ->
              when(result) {
                  is Result.Success -> {
                    // Push Notifications Service Enabled!
                  }

                  is Error -> Log.e("Error", "Error starting Push Notifications")
              }
          }
      }

      is Error -> Log.e("Error", "Error while connecting with Chat Manager")
  }
}