Integrate the MobilePush Android SDK

To use any MobilePush functionality in your app, integrate the MobilePush Android SDK and its dependencies into your app. The integration involves adding the SDK repository within dependencyResolutionManagement in settings.gradle, and the marketingcloudsdk dependency in your app’s module-level build.gradle file, as shown in this example.

To use the latest SDK version, replace {currentVersion} in this code sample with the latest version number available on the SDK Releases page.

Note

1dependencyResolutionManagement {
2    repositories {
3        maven { url "https://salesforce-marketingcloud.github.io/MarketingCloudSDK-Android/repository" }
4        // other repositories
5    }
6}
7
8dependencies {
9  implementation 'com.salesforce.marketingcloud:marketingcloudsdk:{currentVersion}'
10}

The MobilePush SDK no longer declares the Google Play Services Location or AltBeacon libraries as required dependencies for Android app. If your application requires these features, enable them.

Important

Set Up Firebase 

Set up Firebase Cloud Messaging by following the Android Firebase Setup documentation. When you add the Firebase core dependency to your module’s build.gradle file, use com.google.firebase:firebase-core:23.x.x.

If you initialize the FirebaseApp manually, you must initialize Firebase before initializing the SDK.

Important

Initialize the SDK 

Initialize the SDK during the execution of the onCreate method for your Application class. Initialization requires configuration data for your Marketing Cloud Engagement application. For guidance on where to find the required account-related information see Retrieve Required SDK Configuration Data.

For MobilePush SDK version 10 or higher, use this code.

10.x or higher
1class MyApplication : Application() {
2  override fun onCreate() {
3    super.onCreate()
4
5    // Initialize logging _before_ initializing the SDK to avoid losing valuable debugging information.
6    if(BuildConfig.DEBUG) {
7      SFMCSdk.setLogging(LogLevel.DEBUG, LogListener.AndroidLogger())
8      MarketingCloudSdk.setLogLevel(MCLogListener.VERBOSE)
9      MarketingCloudSdk.setLogListener(MCLogListener.AndroidLogListener())
10    }
11
12    SFMCSdk.configure(applicationContext as Application, SFMCSdkModuleConfig.build {
13      engagementModuleConfig = MarketingCloudConfig.builder().apply {
14          setApplicationId("{mc_application_id}")
15          setAccessToken("{mc_access_token}")
16          setMarketingCloudServerUrl("{marketing_cloud_url}")
17          // Other configuration options
18      }.build(applicationContext)
19    }) { initStatus ->
20      // Handle initialization status
21    }
22  }
23}

From MobilePush SDK version 8 or 9, use SFMCSdk to initialize the MarketingCloudSdk.

8.x or 9.x
1class MyApplication : Application() {
2  override fun onCreate() {
3    super.onCreate()
4
5    // Initialize logging _before_ initializing the SDK to avoid losing valuable debugging information.
6    if(BuildConfig.DEBUG) {
7      SFMCSdk.setLogging(LogLevel.DEBUG, LogListener.AndroidLogger())
8      MarketingCloudSdk.setLogLevel(MCLogListener.VERBOSE)
9      MarketingCloudSdk.setLogListener(MCLogListener.AndroidLogListener())
10    }
11
12    SFMCSdk.configure(applicationContext as Application, SFMCSdkModuleConfig.build {
13      pushModuleConfig = MarketingCloudConfig.builder().apply {
14        setApplicationId("{mc_application_id}")
15        setAccessToken("{mc_access_token}")
16        setSenderId("{fcm_sender_id}")
17        setMarketingCloudServerUrl("{marketing_cloud_url}")
18        setNotificationCustomizationOptions(
19          NotificationCustomizationOptions.create(R.drawable.ic_notification_icon)
20        )
21        // Other configuration options
22      }.build(applicationContext)
23    }) { initStatus ->
24      // TODO handle initialization status
25    }
26  }
27}

If you use version 7 of the SDK, use this code.

7.x
1class MyApplication : Application() {
2
3  override fun onCreate() {
4    super.onCreate()
5
6    // Initialize logging _before_ initializing the SDK to avoid losing valuable debugging information.
7    if(BuildConfig.DEBUG) {
8      MarketingCloudSdk.setLogLevel(MCLogListener.VERBOSE)
9      MarketingCloudSdk.setLogListener(MCLogListener.AndroidLogListener())
10    }
11
12    MarketingCloudSdk.init(applicationContext as Application, MarketingCloudConfig.builder().apply {
13      setApplicationId("{mc_application_id}")
14      setAccessToken("{mc_access_token}")
15      setSenderId("{fcm_sender_id}")
16      setMarketingCloudServerUrl("{marketing_cloud_url}")
17      setMid("{mid}")
18      setNotificationCustomizationOptions(
19        NotificationCustomizationOptions.create(R.drawable.ic_notification_icon)
20      )
21      // Other configuration options
22    }.build(applicationContext)) { status ->
23      // TODO handle initialization status
24    }
25  }
26}

If you don’t call the SDK’s configure method and initialize the pushModuleConfig from your app’s onCreate method, the SDK can’t post notifications while the app is in the background.

To troubleshoot the InitializationStatus returned during the SDK’s initialization call, see Troubleshoot Initialization and Registration.

You can customize the display and handling of notifications. For guidance on customizing notification, see Customize Push Notification Functionality for Android Apps.

Request Runtime Notification Permission on Android 13+ 

Starting with Android 13, a new runtime permission is required to display notifications. This change impacts all applications that run on Android 13 or later. Your app can’t display notifications until the user grants this permission. For more information, see Request runtime permissions on the Android Developers website.

After the user accepts the request to display notifications, notify the SDK using this code.

8.x
1SFMCSdk.requestSdk { sdk ->
2  sdk.mp {
3    it.pushMessageManager.enablePush()
4  }
5}

Send a Test Push Notification 

Send a test push notification from Engagement to your app by following the instructions provided in Send a Test Push.