Migrating to Firebase

🚧

GCM is going away April 2019

Make sure you are ready to move to FCM if you haven't already! See Google's blog for more information.

Our Android SDK (v1.3.3+) supports Firebase Cloud Messaging, the new Google Cloud Messaging.

You can continue to use GCM for now — Google will continue to support it and so will Leanplum —but all new features from Google will only be added to Firebase. Also, all new Android apps must use FCM. (See Google's FAQs for Cloud Messaging.)

So, it's probably a good idea to migrate your app over to Firebase.

Import your GCM project into Firebase

The first step is to migrate your existing Android app to FCM using Google's Firebase Console:

  1. In the Firebase console, select Import Google Project.
898
  1. Select your GCM project from the list of existing projects and select Add Firebase.
  2. In the Firebase welcome screen, select Add Firebase to your Android App.
  3. Provide your package name and SHA-1, and select Add App. A new google-services.json file for your Firebase app is downloaded.
1384
  1. Select Continue and follow the detailed instructions for adding the Google Services plugin in Android Studio.

Add the Google JSON file to your app/ folder

Be sure to copy the file created in the steps above to your project's module folder, which is typically app/.

508

Update your project-level build.gradle

buildscript {
    ...
    dependencies {
        // For Firebase
        classpath 'com.google.gms:google-services:4.2.0'
    }
}

Update your app-level build.gradle

Replace the GCM dependency with FCM. And apply the google-services plugin.

dependencies {
  // remove this line...
  // implementation 'com.google.android.gms:play-services-gcm:+'

  // Replace it with these. Be sure you have versions 10.0.1 of play services.
  implementation 'com.google.firebase:firebase-messaging:17.5.0'
  implementation 'com.leanplum:leanplum-fcm:5.2.0'

}

Update your app's manifest

You can remove the GCM receivers and services from your manifest, since FCM will add the correct receiver automatically.


<!-- This is no longer needed -->
<!-- 
<receiver
    android:name="com.google.android.gms.gcm.GcmReceiver"
    android:exported="true"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="[YOUR_PACKAGE_NAME]" />
    </intent-filter>
</receiver> 
-->

Our Android SDK versions 2.1.0+ support manifest merging, so your manifest can be as simple as this:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="[com.YOUR_PACKAGE]"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >

    <uses-sdk tools:overrideLibrary="com.leanplum, com.google.android.gms"/>

    <!-- Optional. Prevents the device from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <!-- These permissions are required only for geofencing. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>   
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".[YOUR_APPLICATION_CLASS]" >
           <meta-data
               android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version" />
        ...
    </application>
</manifest>

You can view the services and permissions our SDK adds to your manifest during the merge in our source code here. If you choose to use an older version of our SDK, or install our SDK using the JAR file, you'll need to manage the manifest yourself. See an example of a manifest.

Add your FCM Server Key to Leanplum

In the Firebase console, click the gear icon next to Overview, then click Project Settings.

1118

In your project's settings, go to the Cloud Messaging tab. In this section of your settings, you will see your Server key. Copy the key.

1986

In the Leanplum dashboard, in your App Settings click Keys & Settings. Go to the Push Notifications tab and enter/paste your key into the Google API key field.

1441

Testing

📘

Not all Android Virtual Devices (AVDs) support Firebase. This level of support does not always match the SDK support (i.e. you can be running a supported SDK, but the AVD will not be able to receive push notifications). If you plan to test Firebase on an Android Studio emulator, be sure to review Google's list of supported AVDs for Firebase.

Additional resources

For more complex app setups, and other documentation, you may want to refer to Google's FCM migration guide.