Configure the React Native Plugin for Android Apps

After you install and configure the React Native plugin, configure the plugin to enable push support for the Android platform.

For new React Native integrations, use the React Native Unified Plugin Setup.

This topic is intended for customers who use the existing React Native plugin for Marketing Cloud Engagement.

Important

Prerequisites 

Before you configure the React Native plugin for Android apps, make sure that the plugin is installed and configured for your app. See Marketing Cloud React Native Plugin.

Add the Repository 

Add the repository to your build.gradle file.

build.gradle
1allprojects {
2  repositories {
3    maven { url "https://salesforce-marketingcloud.github.io/MarketingCloudSDK-Android/repository" }
4    //... Other repos
5  }
6}

Provide FCM Credentials 

To enable push support for Android, provide your FCM credentials by placing the google-services.json file in the android/app directory.

  1. Download the google-services.json file from your app’s Firebase console and place it in your project’s android/app directory.

  2. Include the Google Services plugin in your android/build.gradle file.

    build.gradle
    1buildscript {
    2  repositories {
    3    google() // Google’s Maven repository
    4  }
    5
    6  dependencies {
    7    // ...
    8    // Add this line:
    9    classpath 'com.google.gms:google-services:4.3.15'
    10  }
    11}
  3. Add the plugin to your build.gradle file.

    build.gradle
    1// Add the google services plugin to your build.gradle file
    2apply plugin: 'com.google.gms.google-services'

Configure the SDK 

Configure the SDK in your main application class.

React Native templates default to Kotlin (MainApplication.kt). However, older templates still support Java (MainApplication.java). When you configure the SDK, select the file type that matches your project.

Note

1import android.content.Context
2import android.util.Log
3import com.salesforce.marketingcloud.MarketingCloudConfig
4import com.salesforce.marketingcloud.notifications.NotificationCustomizationOptions
5import com.salesforce.marketingcloud.sfmcsdk.SFMCSdk
6import com.salesforce.marketingcloud.sfmcsdk.SFMCSdkModuleConfig
7
8override fun onCreate() {
9  super.onCreate()
10
11  SFMCSdk.configure(this as Context, SFMCSdkModuleConfig.build {
12      pushModuleConfig = MarketingCloudConfig.builder()
13          .setApplicationId("{MC_APP_ID}")
14          .setAccessToken("{MC_ACCESS_TOKEN}")
15          .setSenderId("{FCM_SENDER_ID_FOR_MC_APP}")
16          .setMarketingCloudServerUrl("{MC_APP_SERVER_URL}")
17          .setNotificationCustomizationOptions(
18              NotificationCustomizationOptions.create(R.drawable.ic_notification)
19          )
20          .setAnalyticsEnabled(true)
21          .build(this@MainApplication)
22  }) { initializationStatus ->
23      Log.e("TAG", "STATUS $initializationStatus")
24      if (initializationStatus.status == 1) {
25          Log.e("TAG", "STATUS SUCCESS")
26      }
27  }
28
29  // ... The rest of the onCreate method
30}
View Java code example
1import android.content.Context;
2import android.util.Log;
3import com.salesforce.marketingcloud.MarketingCloudConfig;
4import com.salesforce.marketingcloud.notifications.NotificationCustomizationOptions;
5import com.salesforce.marketingcloud.sfmcsdk.SFMCSdk;
6import com.salesforce.marketingcloud.sfmcsdk.SFMCSdkModuleConfig;
7
8@Override
9public void onCreate() {
10  super.onCreate();
11
12  SFMCSdk.configure((Context) this, SFMCSdkModuleConfig.build(builder -> {
13      builder.setPushModuleConfig(MarketingCloudConfig.builder()
14              .setApplicationId("{MC_APP_ID}")
15              .setAccessToken("{MC_ACCESS_TOKEN}")
16              .setSenderId("{FCM_SENDER_ID_FOR_MC_APP}")
17              .setMarketingCloudServerUrl("{MC_APP_SERVER_URL}")
18              .setNotificationCustomizationOptions(NotificationCustomizationOptions.create(R.drawable.ic_notification))
19              .setAnalyticsEnabled(true)
20              .build(this));
21
22      return null;
23  }), initializationStatus -> {
24      Log.e("TAG", "STATUS "+initializationStatus);
25      if (initializationStatus.getStatus() == 1) {
26          Log.e("TAG", "STATUS SUCCESS");
27      }
28      return null;
29  });
30
31    // ... The rest of the onCreate method
32}

To customize push notification functionality for Android apps, see the notification customization guide.

To implement the Carousel and Button actions features introduced in version 9 of the SDK, see Configure Button and Carousel Actions.