Android: Updating to Version 6+

If you are updating from an older version of the Android SDK to version 6+, there are certain things you will need to check to ensure that your app will still receive push notifications.

  1. The app has a project set up in the Firebase Console.
  2. The app has a google-services.json file downloaded from the Firebase Console project.
  3. The debug and release SHA-1 values for the app have been added to the Firebase Console project.
  4. The app has the Google Services plugin applied in the build.gradle file.
  5. The Android Manifest has been updated.
  6. Gradle dependencies are up to date.
  7. FirebaseMessagingService implementation.

Firebase Console

In order to use Firebase in your app, it needs to have a project set up in the Firebase Console. If you are still working from the Google Cloud Console, please see our guide to migrating to Firebase.

Google Services JSON

You can download a google-services.json file for your app from the Firebase Console. This needs to be added to the root directory of your app. The Firebase SDK then uses it to access details about your app and its respective Firebase Console project. If you don't already have it, you can download it from the project settings:

In the General tab you should be able to find your app in the 'Your apps' section. You should then be able to download the JSON file from the 'Download the latest config file' section:

More information about adding the the google-services.json file to your app can be found in Google's developer documentation here.

Google Services

The Firebase SDK requires Google Services in the app. To provide this the Google Services dependency needs to be added to your project level build.gradle file:

buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.0.1'
    }
}

The Google Services plugin also needs to be applied in your app's build.gradle file. This should be added to the bottom of the app-level build.gradle file:

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

Remove Unnecessary Values from AndroidManifest

The Firebase SDK handles permissions itself, so you can (and should) remove these values from your app's AndroidManifest.xml. leaving some of these values in the manifest can lead to crashes.

🚧

Leaving some of these values in the manifest can lead to crashes. Make sure you remove them all!

Between the <manifest> </manifest> tags, delete the following values:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />

<permission
  android:name="${applicationId}.permission.C2D_MESSAGE"
  android:protectionLevel="signature" />

Between the <application> </application> tags, delete these values:

<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" />
      <category android:name="${applicationId}" />
  </intent-filter>
</receiver>

<meta-data
  android:name="com.google.android.gms.version"
  android:value="@integer/google_play_services_version" />

Gradle Dependencies

The Android SDK is now utilising the Firebase messaging library. Since the version is greater than version 15.0.0, there is a requirement that all play services and firebase libraries included in your app also be at least version 15.0.0. This requirement was put in place by Google when they moved to semantic versioning from release 15.0.0 onwards. Further details can be found here.

The Firebase messaging library and play services GCM library cannot be present in the same application, so you should ensure the play services GCM library is removed from the build.gradle file.

If you do not include the Firebase Core library in your app dependencies you may receive a Java Compiler warning:

Warning: The app gradle file must have a dependency on com.google.firebase:firebase-core for Firebase services to work as intended.

Firebase messaging notifications will still be received without adding the core library, so it is not required for the Marigold SDK to work correctly, however some of the additional functionality Firebase offers in the console may not work without it.

FirebaseMessagingService Implementation

Firebase sends tokens and notifications to the app through an implementation of the FirebaseMessagingService class. The SDK implements this class internally so your app can work with Firebase without having to create its own implementation. If, however, your app does have an implementation of the FirebaseMessagingService class then it will override the one that is implemented in the SDK. In this case in order for the SDK to function correctly you must pass the token and any received notifications to the SDK manually through the setDeviceToken() and handleNotification() methods:

import com.marigold.sdk.Marigold;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

/**
 * Your app's implementation of FirebaseMessagingService.
 */
public final class AppFirebaseService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
      
      	// Your code for handling notification
      
      	// Pass notification through to Marigold
        new Marigold().handleNotification(remoteMessage);
    }

    @Override
    public void onNewToken(String token) {
      	
      	// Your code for handling token
      
      	// Pass token through to Marigold
        new Marigold.setDeviceToken(token);
    }
}
import com.marigold.sdk.Marigold
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

/**
 * Your app's implementation of FirebaseMessagingService.
 */
class AppFirebaseService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {

        // Your code for handling notification

        // Pass notification through to Marigold
        Marigold().handleNotification(remoteMessage)
    }

    override fun onNewToken(token: String) {

        // Your code for handling token

        // Pass token through to Marigold
        Marigold().setDeviceToken(token)
    }
}

πŸ“˜

Note

Prior to version 10.1.0 you will need to use the static methods on the legacy 'Carnival' class instead of the 'Marigold' class.