Modify behavior of Firebase In-App Messaging messages

With little to no coding effort, Firebase In-App Messaging allows you to create, configure and target rich user interactions, leveraging the capabilities of Google Analytics out of the box to tie messaging events to actual user characteristics, activities, and choices. With some additional Firebase In-App Messaging SDK integration, you can tailor the behavior of in-app messages even further, responding when users interact with messages, triggering message events outside the Analytics framework, and allowing users to control sharing of their personal data related to messaging interactions.

Respond when users interact with in-app messages

With actions you can use your in-app messages to direct users to a website or a specific screen in your app.

Your code can respond to basic interactions (clicks and dismissals), to impressions (verified views of your messages), and to display errors logged and confirmed by the SDK. For example, when your message is composed as a Card modal, you might want to track and follow-up on which of two URLs the user clicked on the Card.

Implement an event listener for Card interactions

The developer workflow is to implement an event listener class adhering to the interface that corresponds to the user activity (viewing impressions, clicking messages) or to a display error, then register the event listener with the FirebaseInAppMessaging instance.

Assuming again that you want to track which link a user clicked on a Card-style message, define a class that implements the messageClicked method per the FirebaseInAppMessagingClickListener interface, thereby providing you access to the in-app messaging Action object, which in turn exposes the URL in the link clicked by the user.

First, implement a click listener:

Kotlin+KTX

class MyClickListener : FirebaseInAppMessagingClickListener {

    override fun messageClicked(inAppMessage: InAppMessage, action: Action) {
        // Determine which URL the user clicked
        val url = action.actionUrl

        // Get general information about the campaign
        val metadata = inAppMessage.campaignMetadata

        // ...
    }
}

Java

public class MyClickListener implements FirebaseInAppMessagingClickListener {

    @Override
    public void messageClicked(InAppMessage inAppMessage, Action action) {
        // Determine which URL the user clicked
        String url = action.getActionUrl();

        // Get general information about the campaign
        CampaignMetadata metadata = inAppMessage.getCampaignMetadata();

        // ...
    }

}

Then register it:

Kotlin+KTX

val listener = MyClickListener()
Firebase.inAppMessaging.addClickListener(listener)

Java

MyClickListener listener = new MyClickListener();
FirebaseInAppMessaging.getInstance().addClickListener(listener);

Refer to the appropriate Android Listener interface reference, FirebaseInAppMessagingImpressionListener, FirebaseInAppMessagingClickListener, or FirebaseInAppMessagingDisplayErrorListener for the callback methods that can be implemented and their parameters.

Trigger in-app messages programmatically

Firebase In-App Messaging by default allows you to trigger in-app messages with Google Analytics for Firebase events, with no additional integration. You can also manually trigger events programmatically with the Firebase In-App Messaging SDK’s programmatic triggers.

In the In-App Messaging campaign composer, create a new campaign or select an existing campaign, and in the Scheduling step of the composer workflow, note the event ID of a newly-created or existing messaging event. Once noted, instrument your app to trigger the event by its ID.

Kotlin+KTX

// somewhere in the app's code
Firebase.inAppMessaging.triggerEvent("exampleTrigger")

Java

// somewhere in the app's code
FirebaseInAppMessaging.getInstance().triggerEvent("exampleTrigger");

Use campaign custom metadata

In your campaigns, you can specify custom data in a series of key/value pairs. When users interact with messages, this data is available for you to, for example, display a promo code.

Kotlin+KTX

class MyClickListenerBundles : FirebaseInAppMessagingClickListener {

    override fun messageClicked(inAppMessage: InAppMessage, action: Action) {
        // Determine which URL the user clicked
        val url = action.actionUrl

        // Get data bundle for the inapp message
        val dataBundle: Map<String, String>? = inAppMessage.data

        // ...
    }
}

Java

public class MyClickListenerBundles implements FirebaseInAppMessagingClickListener {

    @Override
    public void messageClicked(InAppMessage inAppMessage, Action action) {
        // Determine which URL the user clicked
        String url = action.getActionUrl();

        // Get data bundle for the inapp message
        Map<String, String> dataBundle = inAppMessage.getData();

        // ...
    }

}

Temporarily disable in-app messages

By default, Firebase In-App Messaging renders messages whenever a triggering condition is satisfied, regardless of an app's current state. If you'd like to suppress message displays for any reason, for example to avoid interrupting a sequence of payment processing screens, you can do that with the SDK's setMessagesSuppressed method:

Kotlin+KTX

Firebase.inAppMessaging.setMessagesSuppressed(true)

Java

FirebaseInAppMessaging.getInstance().setMessagesSuppressed(true);

Passing true to the method prevents Firebase In-App Messaging from displaying messages, while false reenables message display. The SDK turns off message suppression on app restart. Suppressed messages are ignored by the SDK. Their trigger conditions must be met again while suppression is off before Firebase In-App Messaging can display them.

Enable opt-out message delivery

By default, Firebase In-App Messaging automatically delivers messages to all app users you target in messaging campaigns. To deliver those messages, the Firebase In-App Messaging SDK uses Firebase installation IDs to identify each user's app. This means that In-App Messaging has to send client data, linked to the installation ID, to Firebase servers. If you'd like to give users more control over the data they send, disable automatic data collection and give them a chance to approve data sharing.

To do that, you have to disable automatic initialization for Firebase In-App Messaging, and initialize the service manually for opt-in users:

  1. Turn off automatic initialization with a meta-data tag in your AndroidManifest.xml file:

    <meta-data android:name="firebase_inapp_messaging_auto_data_collection_enabled" android:value="false" />
  2. Initialize Firebase In-App Messaging for selected users manually:

    Kotlin+KTX

    // Only needed if firebase_inapp_messaging_auto_data_collection_enabled is set to
    // false in AndroidManifest.xml
    Firebase.inAppMessaging.isAutomaticDataCollectionEnabled = true

    Java

    // Only needed if firebase_inapp_messaging_auto_data_collection_enabled is set to
    // false in AndroidManifest.xml
    FirebaseInAppMessaging.getInstance().setAutomaticDataCollectionEnabled(true);

    Once you set a data collection preference manually, the value persists through app restarts, overriding the value in your AndroidManifest.xml. If you'd like to disable initialization again, for example if a user opts out of collection later, pass false to the setAutomaticDataCollectionEnabled method.