Firebase

Firebase in-app messaging android

In this post, we gonna talk about firebase in-app messaging in android.

Overview

Firebase In-App Messaging helps you engage your app’s active users by sending them targeted, contextual message. Encourage them to use key app features. For example, you can send an in-pp message to get users to subscribe, watch a video, complete a level, or buy an items. I’m sure you guys familiar. If you had android app you often get in-app message.

You can customized message as card, banners, models, or images. You can also set up a trigger so that they appear exactly when they’d benefit. It is very similar to cloud messaging, except for messages sent to the app, not a device. So if you talk about cloud messaging.

Uses of In-App Messaging

You should use Firebase In-App Messaging to encourage exploration and discovery. Highlight a sale or coupon in your eCommerce app. Give clues or tips in your game. Prompt a like or share in your social media app. These types of things, So kind of different use case or messaging.

Key Capabilities of In App Messaging

  • Send relevant, engaging messages
  • Sends messages when they are most needed, while users are actually in your app.
  • Promote a big sale when users visit your In-app store. etc.
  • Target messages by audience or behavior.
    • Work with Analytics and Predictions to give you tools to deliver the message to the user you would most like to reach
    • Send messages based on user’s demographics, past behaviors, or even predictions of their future behaviors.
  • Create flexible, custom alerts –
    • You can create a flexible, custom alert and have the ability to customize your style appearance display triggers and content.
    • These will help you do everything from sending promotional offers to getting users to update a new version of the app.

Implementations Step

Now I want to show you, how you can setup and configure to use In App Messaging for Firebase.

  • Connect your app – Start by adding Firebase to your app in the firebase console
  • Integrates the SDK – Add the firebase In-App Messaging SDK to your app via Gradle.
  • Create your first message – Visit the Firebase console to write, customize, and target your first message.

Connect your app

Let’s open android studio, and create a simple project. For now It doesn’t have anything extra on it. It just MainActivity that’s empty.

The first thing we’re gonna to do we’re going to add this project to Firebase. Fo doing that we need to know is the package name, In this example, I’m using com.firebaseinappmessaging .

Now let’s go to the firebase console and go to the project. Then just add the app and go through the steps the adding an app by entering package name. Still, you are facing any issue, follow this article.

Now you have to download json file, So go to settings and download on google-service.json file in app folder.

Integrates the SDK

Now back to the Android Studio, Now we have to update Gradle file. So go to the project build.gradle file and add these dependencies. Something like below

    // Import the Firebase BoM
    implementation 'com.google.firebase:firebase-inappmessaging-display-ktx:19.1.5'
    implementation 'com.google.firebase:firebase-analytics-ktx:18.0.2'

Let’s sync the project, That all about configuration. For sending a test message you have to need AppInstaceId, Basically firebase console allow us send messaging to specific device.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        android:text="@string/warning_fresh_install"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@+id/instanceIdText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/instanceIdText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/warning_fresh_install"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        tools:text="Device Instance ID: 1234" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/eventTriggerButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:background="@color/purple_200"
        android:text="@string/button_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.firebaseinappmessaging

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.google.android.gms.tasks.OnSuccessListener
import com.google.firebase.inappmessaging.FirebaseInAppMessaging
import com.google.firebase.inappmessaging.display.ktx.FirebaseInAppMessagingDisplayKtxRegistrar
import com.google.firebase.installations.FirebaseInstallations
import com.google.firebase.ktx.Firebase
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private lateinit var firebaseIam: FirebaseInAppMessaging
    //private lateinit var firebaseAnalytics: FirebaseAnalytics
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // firebaseAnalytics = FirebaseAnalytics.getInstance(this)
        firebaseIam = FirebaseInAppMessaging.getInstance()

        firebaseIam.isAutomaticDataCollectionEnabled = true
        firebaseIam.setMessagesSuppressed(false)

        eventTriggerButton.setOnClickListener { view ->
            //firebaseAnalytics.logEvent("engagement_party", Bundle())
            firebaseIam.triggerEvent("engagement_party")
        }

        // Get and display/log the Instance ID
        FirebaseInstallations.getInstance().id.addOnSuccessListener {
            instanceIdText.text = getString(R.string.instance_id_fmt, it)
            Log.d(TAG, "InstanceId: $it")
        }
//            .addOnSuccessListener(object : OnSuccessListener<InstanceIdResult> {
//                override fun onSuccess(instanceIdResult: InstanceIdResult) {
//                    val instanceId = instanceIdResult.id
//                    instanceIdText.text = getString(R.string.instance_id_fmt, instanceId)
//                    Log.d(TAG, "InstanceId: $instanceId")
//                }
//            })

        addClickListener()
    }
    private fun addClickListener() {
        val listener = InAppMessageClickListener()
        firebaseIam.addClickListener(listener)
    }

    private fun suppressMessages() {
        firebaseIam.setMessagesSuppressed(true)
    }

    private fun enableDataCollection() {
        // Only needed if firebase_inapp_messaging_auto_data_collection_enabled is set to
        // false in AndroidManifest.xml
        firebaseIam.isAutomaticDataCollectionEnabled = true
    }

    companion object {

        private const val TAG = "FirebaseInAppMessaging"
    }
}
package com.example.firebaseinappmessaging

import android.util.Log
import com.google.firebase.inappmessaging.FirebaseInAppMessagingClickListener
import com.google.firebase.inappmessaging.model.Action
import com.google.firebase.inappmessaging.model.InAppMessage


class InAppMessageClickListener : 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

        Log.d("AppTest", "URL is: " + url)
        Log.d("AppTest", "Meta Data: " + metadata)
    }
}

Subscribe
Notify of
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

cool