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.
To help you find your way around, this video mirrors the steps listed below.
The first step is to go your Firebase console. Sign up if you haven't already.
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.
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.
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.
Have you downloaded the google-services.json
config file from your Firebase project console? If not, see this video.
Move your google-services.json
config file into your Android Studio project, in the app
directory:
YOUR_ANDROID_PROJECT/app/google-services.json
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.2.0'
// 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
}
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:18.0.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 by pressing the "Sync Now" button:

In order to provide push notification functionality, the SDK needs a reference to your application's Context
1 2 3 4 5 6 7 8 9 10 11
val chatManager = ChatManager( instanceLocator = "YOUR_INSTANCE_LOCATOR", userId = "YOUR_USER_ID", dependencies = AndroidChatkitDependencies( tokenProvider = ChatkitTokenProvider( endpoint = "YOUR_AUTH_URL", userId = "YOUR_USER_ID" ), context = Activity.getApplicationContext() // required for push notifications to work ) )
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") } }
We can customise what happens when a person taps their push notification, for example you might want to load that conversation directly. You will need to pull out the intent extras in the launcher activity. Currently that's provided as a JSON string, which we'll need to parse. We recommend using gson. The extra is a String that looks like the following:
1
"{"roomId":"<room_id_here>"}"
Next we need to create ourselves a data class to parse our notification response into:
1
data class ChatkitPushNotificationResponse(val roomId: String)
Finally, in the launcher activity (check your Android Manifest if you're uncertain), in the onCreate
you can add the following code to get the sender room id.
1 2 3 4 5 6 7 8 9 10
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val senderInfoJson = intent.getStringExtra("chatkit") if (senderInfoJson != null) { var senderInfo = Gson().fromJson(senderParts, ChatkitPushNotificationResponse::class.java) Log.d("TAG", "sender was " + entity.roomId) } }
You must explicitly disable push notifications for the current user when they sign out. In addition, you should also disable push notifications when the app launches and you're not in a logged in state. If you don't, people will receive notifications after they have signed out which will be unexpected. This will also prevent them from receiving push notifications if they sign in as a different user in future!
In your block of code where you sign your user out, call the following:
1
chatManager.disablePushNotifications { }