/salesforce-marketing-cloud

Salesforce Marketing Cloud MobilePush Flutter SDK

Primary LanguageSwiftMIT LicenseMIT

sfmc_plugin

pub package

Salesforce Marketing Cloud (SFMC) - MobilePush SDK for Flutter.

Marketing Cloud MobilePush lets you create and send notifications to encourage use of your app.

This SFMC Flutter Plugin includes:

  • Push Notifications

    Create a message using one of the templates in Marketing Cloud MobilePush or the Push Notification template in Content Builder or Journey Builder.

  • In-App Messages

    In-app messages allow you to interact with your mobile app users while they use your mobile app, which is when they are most engaged. Trigger a message to show in your mobile app when a user opens the app on their device. While you can send this message to all of a contact’s devices, it only shows on the first device that opens the app. In-app messages can’t be created in MobilePush.

Learn more about SFMC Mobile Push SDK:

Let's get started

First of all you need to add sfmc_plugin in your project. In order to do that, follow this guide.

We suggest you to check example source code.

Setup Android

Create an app in MobilePush. This process connects the device to the MobilePush app you created previously in MobilePush.

Note: The Android SDK requires Android API 21 or greater and has dependencies on the Android Support v4 and Google Play Services libraries.

Set up Firebase

Follow the Android Firebase setup documentation.

And your Firebase google-services.json to your android/app folder.

Add google-services plugin to your android/app/build.gradle:

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

Add google-services dependency to android/build.gradle:

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

Initialize SFMC SDK in Android

Create a custom MainActivity.kt which is used instead of the default FlutterApplication (e.g.: https://stackoverflow.com/a/49084400/3410232).

Then do not extend FlutterApplication but BaseApplication from com.sfmc_plugin. Now you can either provide your SFMC config values:

Please update android/gradle.properties as example below:

MC_APP_ID="<YOUR_SFMC_APP_ID>"
MC_ACCESS_TOKEN="<YOUR_SFMC_ACCESS_TOKEN>"
MC_SENDER_ID="<YOUR_FIREBASE_CLOUD_MESSAGING_SENDER_ID_FOR_SFMC>"
MC_MID="<YOUR_SFMC_MID>"
MC_SERVER_URL="<YOUR_SFMC_URL>"

or to allow for more flexibility override the configBuilder

override val configBuilder: MarketingCloudConfig.Builder?
        get() = MarketingCloudConfig.builder().apply { ... }

as you can see the configBuilder can also be null. This allows your application to not initialize the SDK if you do not want to. See this thread for a very common use case: https://salesforce.stackexchange.com/questions/325537/changing-the-marketingcloudconfig-later-in-the-apps-lifecycle

Setup iOS

Provision for Push

Please follow the Provision for Push.

Update info.plist

Note: Please add UIBackgroundModes keys into your info.plist file as below:

<key>UIBackgroundModes</key>
	<array>
    	<string>fetch</string>
    	<string>remote-notification</string>
	</array>

Setup UNUserNotificationCenter delegate

Add the following lines to the application method in the AppDelegate.m/AppDelegate.swift file of your iOS project.

Objective-C:

if (@available(iOS 10.0, *)) {
  [UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
}

Swift:

if #available(iOS 10.0, *) {
  UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}

Use Flutter Plugin

The first step is initializing the SFMC plugin with credential information (applies to iOS only, for Android please refer to setup Android section). Learn more

    /// iOS only
    var isInitialized = await SfmcPlugin().initialize(
        appId: '<YOUR_APP_ID>',
        accessToken: '<YOUR_ACCESS_TOKEN>',
        mid: '<YOUR_MID>',
        sfmcURL:
            '<YOUR_SFMC_URL>',
        senderId: '<YOUR_FIREBASE_CLOUD_MESSAGING_SENDER_ID>',

        /// Set delayRegistration on iOS only,
        /// delayRegistration on Android is by default true
        delayRegistration: true,

        /// Set analytics on iOS only,
        /// analytics on Android is by default true
        analytics: true,
      );

The second step is setting your sfmc contact key which is a unique client id in Salesforce Marketing Cloud.

await SfmcPlugin().setContactKey('<Uniquely Identifying Key>');

Attributes

You can use attributes to segment your audience and personalize your messages. Before you can use attributes, create them in your MobilePush account. Attributes may only be set or cleared by the SDK. See the Reserved Words section for a list of attribute keys that can’t be modified by the SDK or your application.

await SfmcPlugin().setProfileAttribute("key", "value");

await SfmcPlugin().clearProfileAttribute("key");

Other methods

This plugin exposes some other methods such as getPushToken and setPushToken, which are helpful if you want to continue using other push providers as per the official documentation: https://salesforce-marketingcloud.github.io/MarketingCloudSDK-Android/trouble-shooting/multiple-push-sdks.html

Contributions

🍺 Pull requests are welcome!

Feel free to contribute to this project.