Configure the MobilePush SDK Flutter Plugin for Android Apps

After you install and configure the MobilePush SDK Flutter plugin, configure the plugin to enable push support for the Android platform.

The implementation steps can vary depending on your app and the Flutter version it uses.

Note

Add the MobilePush SDK Repository 

To add the MobilePush SDK repository to your project, add this code to your android/build.gradle file.

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

Add the MarketingCloud SDK Dependency 

To add the marketingcloudsdk dependency to your project, add this code to your android/app/build.gradle file.

android/app/build.gradle
1dependencies {
2    implementation "com.salesforce.marketingcloud:marketingcloudsdk:9.0.+"
3}

Update Compile and Minimum Android SDK Versions 

In your app’s build.gradle file, set the SDK version parameters to the versions listed in this table.

ParameterVersion
compileSdk or compileSdkVersion35
minSdkVersion23

Update Kotlin Version 

Ensure the org.jetbrains.kotlin.android or org.jetbrains.kotlin:kotlin-gradle-plugin plugin is version 1.9.10 or later.

Depending on your Flutter implementation, the location for specifying the Kotlin version can differ. You can specify the Kotlin version in either android/build.gradle or android/settings.gradle.

Important

Provide Firebase Cloud Messaging Credentials 

To enable push support for the Android platform, provide your Firebase Cloud Messaging (FCM) credentials by including the google-services.json file in your config.xml file.

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

  2. To include the Google Services plugin, add this code to your android/settings.gradle file.

    android/settings.gradle
    1pluginManagement {
    2    buildscript {
    3        repositories {
    4            mavenCentral()
    5        }
    6        dependencies {
    7            classpath 'com.google.gms:google-services:4.3.2'
    8        }
    9    }
    10}

    If pluginManagement isn’t present in your settings.gradle, add this dependency in android/build.gradle under the buildscript section. If the Gradle file doesn’t have a buildscript section, add it.

    Note

  3. To apply the plugin, add this code to your android/app/build.gradle file.

    This step can vary depending on your implementation.

    Note

    android/app/build.gradle
    1plugins {
    2    id "com.google.gms.google-services"
    3}

    If the plugins section doesn’t exist in your android/app/build.gradle file, add this code to the end of the file.

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

Update MainApplication.kt 

If MainApplication.kt isn’t present in your app, create it by extending the FlutterApplication class. Additionally, update AndroidManifest.xml.

Note

AndroidManifest.xml
1// add ".MainApplication" entry in main/AndroidManifest.xml
2
3<application android:name=".MainApplication" ...>

Update MainApplication.kt in your app.

MainApplication.kt
1//MainApplication.kt
2
3//YOUR_package
4
5//rest of imports...
6import android.util.Log
7import com.salesforce.marketingcloud.MarketingCloudConfig
8import com.salesforce.marketingcloud.notifications.NotificationCustomizationOptions
9import com.salesforce.marketingcloud.sfmcsdk.InitializationStatus
10import com.salesforce.marketingcloud.sfmcsdk.SFMCSdk
11import com.salesforce.marketingcloud.sfmcsdk.SFMCSdkModuleConfig
12import io.flutter.app.FlutterApplication
13
14class MainApplication : FlutterApplication() {
15
16    //Update onCreate
17    override fun onCreate() {
18        super.onCreate()
19
20        SFMCSdk.configure(
21            applicationContext,
22            SFMCSdkModuleConfig.build {
23                pushModuleConfig =
24                    MarketingCloudConfig.builder()
25                        .apply {
26                            //Update these details based on your MC config
27                            setApplicationId("{MC_APP_ID}")
28                            setAccessToken("{MC_ACCESS_TOKEN}")
29                            setMarketingCloudServerUrl("{MC_APP_SERVER_URL}")
30                            setSenderId("{FCM_SENDER_ID_FOR_MC_APP}")
31                            setNotificationCustomizationOptions(
32                                NotificationCustomizationOptions.create(
33                                    R.mipmap.ic_launcher
34                                )
35                            )
36                        }
37                        .build(applicationContext)
38            }
39        ) { initStatus ->
40            when (initStatus.status) {
41                InitializationStatus.SUCCESS -> Log.d("SFMC", "SFMC SDK Initialization Successful")
42                InitializationStatus.FAILURE -> Log.d("SFMC", "SFMC SDK Initialization Failed")
43                else -> Log.d("SFMC", "SFMC SDK Initialization Status: Unknown")
44            }
45        }
46
47        //rest of onCreate...
48    }
49
50    //rest of MainApplication...
51}

To customize push functionality, see Customize Push Notification Functionality for Android Apps.

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

Declare Notification Permission 

Update AndroidManifest.xml to declare the notification permission.

AndroidManifest.xml
1//AndroidManifest.xml
2<manifest ...>
3    //Add this line to declare the notification permission.
4    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
5    <application ...>
6        ...
7    </application>
8</manifest>

Handle URLs 

The SDK doesn’t automatically present URLs from these sources.

  • CloudPages URLs from push notifications
  • OpenDirect URLs from push notifications
  • Action URLs from in-app messages

To handle URLs from push notifications, follow these steps.

Navigate to MainApplication.kt and update setNotificationCustomizationOptions to include the code in this example.

  1. Open MainApplication.kt in an editor.

  2. In the setNotificationCustomizationOptions section, add these import statements.

    MainApplication.kt
    1import android.app.PendingIntent
    2import android.content.Context
    3import android.content.Intent
    4import android.net.Uri
    5import android.os.Build
    6import com.salesforce.marketingcloud.notifications.NotificationManager
    7import com.salesforce.marketingcloud.notifications.NotificationMessage
    8import java.util.Random
  3. Update setNotificationCustomizationOptions in MarketingCloudConfig.

    MainApplication.kt
    1//MainApplication.kt
    2
    3setNotificationCustomizationOptions(NotificationCustomizationOptions.create { context: Context, notificationMessage: NotificationMessage ->
    4    NotificationManager.createDefaultNotificationChannel(context).let { channelId ->
    5        NotificationManager.getDefaultNotificationBuilder(
    6            context,
    7            notificationMessage,
    8            channelId,
    9            R.mipmap.ic_launcher
    10        ).apply {
    11            setContentIntent(
    12                NotificationManager.redirectIntentForAnalytics(
    13                    context,
    14                    getPendingIntent(context, notificationMessage),
    15                    notificationMessage,
    16                    true
    17                )
    18            )
    19        }
    20    }
    21})
  4. Implement these methods in MainApplication.kt.

    MainApplication.kt
    1//MainApplication.kt
    2
    3private fun provideIntentFlags(): Int {
    4    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    5        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    6    } else {
    7        PendingIntent.FLAG_UPDATE_CURRENT
    8    }
    9}
    10
    11private fun getPendingIntent(
    12    context: Context,
    13    notificationMessage: NotificationMessage
    14): PendingIntent {
    15    val intent = if (notificationMessage.url.isNullOrEmpty()) {
    16        context.packageManager.getLaunchIntentForPackage(context.packageName)
    17    } else {
    18        Intent(Intent.ACTION_VIEW, Uri.parse(notificationMessage.url))
    19    }
    20    return PendingIntent.getActivity(context, Random().nextInt(), intent, provideIntentFlags())
    21}

Handle URLs from In-App messages 

To handle URLs from in-app messages, set the setUrlHandler in MarketingCloudConfig as shown in this example.

MainApplication.kt
1//MainApplication.kt
2
3// Add import statement for UrlHandler
4import com.salesforce.marketingcloud.UrlHandler
5
6// Tell the SDK how to handle button clicks in an IAM
7setUrlHandler(UrlHandler { context, url, _ ->
8    PendingIntent.getActivity(
9    context,
10    Random().nextInt(),
11    Intent(Intent.ACTION_VIEW, Uri.parse(url)),
12    PendingIntent.FLAG_UPDATE_CURRENT
13    )
14})

Additionally, review the additional documentation on URL Handling for Android.

Troubleshoot Your Android Setup 

If you encounter errors related to Java sealed classes or dexing, add the r8 dependency to your app. For more information about this error, see Google’s IssueTracker.

To add the r8 dependency, update your app’s settings.gradle file.

If you don’t have pluginManagement in your settings.gradle file, update the buildscript.dependencies section of the android/build.gradle file.

Note

settings.gradle
1pluginManagement {
2    buildscript {
3        repositories {
4            mavenCentral()
5        }
6        dependencies {
7            //Add the r8 dependency
8            classpath 'com.android.tools:r8:8.2.42'
9
10            //other dependencies
11        }
12    }
13// rest of settings.gradle
14}