Integrate Android SDK

Once you have properly configured FCM you will need to integrate the Pusher & FCM SDKs into your project. In this guide we will assume that you are using Gradle and Android Studio.

Add Firebase Config File to Your Project

Download the google-services.json config file from your Firebase project console and save it into your Android Studio project:

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:3.1.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
}

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 Beams 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 these lines
    implementation 'com.google.firebase:firebase-messaging:11.8.0'
    implementation 'com.pusher:push-notifications-android:0.10.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

Import Pusher Beams

Go to the Activity or Application class where you want to use Beams and import the SDK:

import com.pusher.pushnotifications.PushNotifications;

Connect to Pusher

Add this line to connect the device to your Beams instance:

PushNotifications.start(getApplicationContext(), "YOUR_INSTANCE_ID");
Your instance ID can be found in the dashboard.

Subscribe to an Interest

Finally, add this line to subscribe to an interest so that your server can send notifications to this device.

PushNotifications.subscribe("hello");

Where Next?

Now that you have integrated the SDK into your Android project why not send a notification?

Did you find this document useful?

We are always striving to create the most accurate and informative docs as possible. If there is something especially wrong (or right) here then please let us know.