Photo by Thought Catalog on Unsplash

Using Firebase In-App-Messaging on an Android app

The many ways to trigger and customize messages

Rosário Pereira Fernandes
ProAndroidDev
Published in
4 min readJul 10, 2020

--

Released almost 2 years ago, In-App-Messaging is a Firebase product that developers can use to send targeted messages (in form of dialogs) to their users. Setting it up and sending a test message is pretty straight forward, but there’s more that can be done to improve the User Experience.
In this blog post, I’ll show some of the different ways to trigger and customize in-app messages.

Getting Started 👶

As mentioned above, sending a test message from Firebase In-App-Messaging (abbreviated as FIAM from this point on) is pretty simple and can be done in 4 steps:

  1. Add the FIAM and Analytics dependencies to your build.gradle(app):
implementation "com.google.firebase:firebase-inappmessaging-ktx:$fiamVersion"
implementation "com.google.firebase:firebase-analytics-ktx:$analyticsVersion"

2. Run the app to find your Installation ID in the logcat:

I/FIAM.Headless: Starting InAppMessaging runtime with Installation ID YOUR_INSTALLATION_ID

3. Go to the In-App Messaging tab on the Firebase Console, click on “New Campaign” and enter a Title for that campaign.

4. Click “Test on your Device”, enter your Installation ID and click on “Test” to send the message.

Once you relaunch the app, you should now see your test message being displayed :)

Now if you wish to send the message to the users of your app, you can go ahead and finish composing that campaign, as explained in the Firebase Documentation.

Seasonal Campaigns 🐰🎄

This is my favorite FIAM feature and perhaps the most used one. It allows you to schedule messages to be shown during a specified time range. So you can use it to inform your users about Easter discounts, Christmas sales or even that great deal that your company is offering during a pandemic. You can also specify how often the message is shown to the user.

The good part is that you don’t need to write any code to achieve that. You can do it all straight from the Firebase Console. When composing your campaign, you’ll come across Step 3 — Scheduling your message. That’s where you can set a start date and an end date.

You’ll notice that the messages are triggered by Analytics events (which is why we added analytics to the app). Two events that are logged automatically by Firebase Analytics are on_foreground and app_launch, so these will always be available for you to choose from when composing a campaign. If you’ve previously logged events to Firebase Analytics, those events will also be available to choose from. Otherwise, you can also create a new event right there.

Trigger messages with Analytics events 💬

Let’s say we have an e-commerce app and we want to show users a dialog when they’re about to perform a checkout. When composing the campaign on the Firebase Console, we’ll set it to only be triggered on the begin_checkout event:

We could log the begin_checkout event to Firebase Analytics when the user clicks on the checkout button:

And our message should be displayed to the user. :)

Alternatively, you can trigger the event without actually logging the data to Firebase Analytics, by calling:

Firebase.inAppMessaging.triggerEvent("begin_checkout")

Customize the message behavior 🔧

Now if the user clicks on the “Get Code” button from our in-app-message, we might want to take an action, such as notifying our backend about the fact that we should now give that user a discount.

We can do that by adding the promo code as custom metadata on Step 5 when we’re composing the campaign on the Console:

And then we can obtain that promo code on the app by adding a click listener to our FIAM instance:

Customize the way it looks 🎨

Maybe you have created a unique and kick-ass custom design for your app dialogs and would like to apply it to your In-App-Message.

You can do so by calling setMessageDisplayComponent() on the onResume() lifecycle event of your Activity and checking what type of message the app received and then get the metadata (title, caption, actions, etc) included so that you can use it on your custom dialog.

And that’s all for now. If you would like to learn more about Firebase In-App-Messaging, you can check the Documentation.

Thanks for reading. If you have thoughts on FIAM or Firebase in general, leave a comment bellow or reach out to me on Twitter and I’ll be happy to have a chat about it. :)

--

--