MobilePush Flutter Plugin
Use the Flutter Plugin to integrate the MobilePush SDK into your Flutter applications.
To install the Flutter plugin, add the plugin to your application using Pub by running the following command.
The steps described in this section can vary depending on your app and the Flutter version it's using.
Navigate to android/build.gradle
and add the MobilePush SDK repository.
Navigate to android/app/build.gradle
and update the dependencies
section to include the marketingcloudsdk
dependency.
In your app's build.gradle
file, ensure that the following SDK version parameters are set to the following versions.
Parameter | Version |
---|---|
compileSdk or compileSdkVersion | 34 |
minSdkVersion | 21 |
Ensure the org.jetbrains.kotlin.android
or org.jetbrains.kotlin:kotlin-gradle-plugin
specified is equal to or above 1.9.10
.
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
.
To enable push support for the Android platform, you must provide your Firebase Cloud Messaging (FCM) credentials by including the google-services.json
file in your config.xml
file.
-
Download the
google-services.json
file from your application's Firebase console and place it in your project'sandroid/app
directory -
Include the Google Services plugin in your
android/settings.gradle
file.If
pluginManagement
isn’t present in yoursettings.gradle
, add this dependency inandroid/build.gradle
under thebuildscript
section. If there’s nobuildscript
section, create it. -
Apply the plugin in the
android/app/build.gradle
file by updating theplugins
section.This step can vary depending on your implementation.
If the
plugins
section doesn’t exist in yourandroid/app/build.gradle
file, add the following code at the end of the file.
If MainApplication.kt
isn't present in your app, create it by extending the FlutterApplication
class. Additionally, update AndroidManifest.xml
.
Update MainApplication.kt
in your app.
Update AndroidManifest.xml
to declare the notification permission.
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
, as shown in the following examples.
-
Add
imports
. -
Update
setNotificationCustomizationOptions
inMarketingCloudConfig
. -
Implement the following methods in
MainApplication.kt
.
To handle URLs from in-app messages, set the setUrlHandler
in MarketingCloudConfig
as shown in the following example.
Additionally, review the additional documentation on URL Handling for Android.
If you encounter errors related to Java sealed classes or dexing, you must 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.
Enable push notifications in your target’s Capabilities settings in Xcode.
-
Navigate to the
YOUR_APP/ios
directory and openRunner.xcworkspace
. -
To configure the SDK and enable push, update
AppDelegate
.
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.
To set the URLHandlingDelegate
, update the AppDelegate
as shown in the following example.
Implement the URLHandlingDelegate
in AppDelegate
, as shown in the following example.
Also review the additional documentation on URL Handling
Rich notifications include images, videos, titles and subtitles from the MobilePush app, and mutable content. Mutable content can include personalization in the title, subtitle, or body of your message.
- In Xcode, click File
- Click New
- Click Target
- Select Notification Service Extension
- Name and save the new extension
This service extension checks for a "_mediaUrl"
element in request.content.userInfo
. If it's present, the extension attempts to download media from the URL, creates a thumbnail-size version, and then adds the attachment. Additionally, the service extension also checks for a "_mediaAlt"
element in request.content.userInfo
. If this key is found, the service extension uses the element for the body text, in case there are any problems downloading or creating the media attachment.
A service extension can time out when it's unable to download media. In the following example, the service extension delivers the original content but replaces the body text with the value from "_mediaAlt"
.
If you encounter a cycle error while enabling rich notifications, see Resolve Cycle Errors in Flutter Xcode Projects.
If you encounter a cycle error in your Flutter Xcode project after adding a Notification Service Extension, follow these steps to fix it.
- Navigate to
YOUR_APP_TARGET
in Xcode. - With your app target selected, go to the Build Phases tab.
- Find Embed Foundation Extension.
- Drag and position it above both Thin Binary and Embed Pods Frameworks.
Reordering the build phases resolves the cycle error.
Kind: Global class
SFMCSdk
.isPushEnabled() ⇒ Future<bool?>
.enablePush() ⇒ Future<void>
.disablePush() ⇒ Future<void>
.getSystemToken() ⇒ Future<String?>
.getAttributes() ⇒ Future<Map<Object?, Object?>?>
.setAttribute(key, value) ⇒ Future<void>
.clearAttribute(key) ⇒ Future<void>
.addTag(tag) ⇒ Future<void>
.removeTag(tag) ⇒ Future<void>
.getTags() ⇒ Future<List<String>>
.setContactKey(contactKey) ⇒ Future<void>
.getContactKey() ⇒ Future<String?>
.enableLogging() ⇒ Future<void>
.disableLogging() ⇒ Future<void>
.logSdkState() ⇒ Future<void>
.trackEvent(event) ⇒ Future<void>
.getDeviceId() ⇒ Future<String?>
.setAnalyticsEnabled(analyticsEnabled) ⇒ Future<void>
.isAnalyticsEnabled() ⇒ Future<bool>
.setPiAnalyticsEnabled(analyticsEnabled) ⇒ Future<void>
.isPiAnalyticsEnabled() ⇒ Future<bool>
The current state of the pushEnabled flag in the native MobilePush SDK.
Kind: Static method of SFMCSdk
Returns: Future<bool?>
- A future to the nullable boolean representation of whether push is enabled.
See Also
Enables push messaging in the native MobilePush SDK.
Kind: Static method of SFMCSdk
See Also
Disables push messaging in the native MobilePush SDK.
Kind: Static method of SFMCSdk
See Also
This method returns the token used by Marketing Cloud Engagement to send push messages to the device.
Kind: Static method of SFMCSdk
Returns: Future<String?>
- A future to the nullable system token string.
See Also
Returns the maps of attributes set in the registration.
Kind: Static method of SFMCSdk
Returns: Future<Map<String, String>>
- A future to the string key-value map of attributes set in the registration.
See Also
Sets the value of an attribute in the registration.
Kind: Static method of SFMCSdk
See Also
Parameter | Type | Description |
---|---|---|
key | string | The name of the attribute to be set in the registration. |
value | string | The value of the key attribute to be set in the registration. |
Clears the value of an attribute in the registration.
Kind: Static method of SFMCSdk
See Also
Parameter | Type | Description |
---|---|---|
key | string | The name of the attribute whose value must be cleared from the registration. |
Adds a tag to the list of tags in the registration.
Kind: Static method of SFMCSdk
See Also
Parameter | Type | Description |
---|---|---|
tag | string | The tag to be added to the registration. |
Removes a tag from the list of tags in the registration.
Kind: Static method of SFMCSdk
See Also
Parameter | Type | Description |
---|---|---|
tag | string | The tag to be removed from the registration. |
Returns the list of tags currently set in the registration.
Kind: Static method of SFMCSdk
Returns: Future<List<String>>
- A future to the list of strings representing the tags
currently set in the registration.
See Also
Sets the contact key for the device's user.
Kind: Static method of SFMCSdk
See Also
Parameter | Type | Description |
---|---|---|
contactKey | string | The contact key to be set for the device's user. |
Returns the contact key associated with the device's user.
Kind: Static method of SFMCSdk
Returns: Future<String?>
- A future to the nullable string representation of the contact key
associated with the device's user.
See Also
Enables verbose logging within the native MobilePush SDK and Unified SFMC SDK.
Kind: Static method of SFMCSdk
See Also
Disables verbose logging within the native MobilePush SDK.
Kind: Static method of SFMCSdk
See Also
Instructs the native SDK to log the SDK state to the native logging system (Logcat for Android and Xcode or Console for iOS). The Engagement Support team requests for the SDK state logs during support calls as they can help diagnose most issues.
Kind: Static method of SFMCSdk
See Also
This method helps to track events, which could result in actions such as an InApp Message being displayed.
Kind: Static method of SFMCSdk
See Also
Parameter | Type | Description |
---|---|---|
event | CustomEvent | EngagementEvent | SystemEvent | CartEvent | OrderEvent | CatalogObjectEvent | The event to track. |
Returns the deviceId used by Engagement to send push messages to the device.
Kind: Static method of SFMCSdk
Returns: Future<String?>
- A future to the device Id.
See Also
Enables or disables analytics in the MobilePush SDK.
Kind: Static method of SFMCSdk
See Also
Parameter | Type | Description |
---|---|---|
analyticsEnabled | boolean | A flag indicating whether to enable analytics. |
Checks if analytics is enabled in the MobilePush SDK.
Kind: Static method of SFMCSdk
Returns: Future<bool>
- A future to the boolean representation of whether analytics is enabled.
See Also
Enables or disables Predictive Intelligence analytics in the MobilePush SDK.
Kind: Static method of SFMCSdk
See Also
Parameter | Type | Description |
---|---|---|
analyticsEnabled | boolean | A flag indicating whether to enable Predictive Intelligence (PI) analytics. |
Checks if Predictive Intelligence analytics is enabled in the MobilePush SDK.
Kind: Static method of SFMCSdk
Returns: Future<bool>
- A future to the boolean representation of whether PI analytics is enabled.
See Also
To use the Flutter APIs, import the SFMCSdk
package into your application as shown in the following example.
Where possible, we changed noninclusive terms to align with our company value of Equality. We retained noninclusive terms to document a third-party system, but we encourage the developer community to embrace more inclusive language. We can update the term when it’s no longer required for technical accuracy.