Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase In-App Messaging showing in SplashActivity. How to show it in MainActivity?

I have enabled Firebase In-App messaging for my android app. When i am testing In-App Messaging it is showing in SplashActivity of the app.

Activity flows like: SplashActivity>LoginActivity>MainActivity

Note: SplashActivity just have runnable to get delay of 3 seconds. LoginActivity have some functions to check wheter shared preferences are not null.

I tried to add in onCreate() this below line of code: FirebaseInAppMessaging.getInstance().setMessagesSuppressed(true)

And FirebaseInAppMessaging.getInstance().setMessagesSuppressed(false) in onDestroy()

I want this messsage to show in MainActivity.

like image 441
Shubham Pandey Avatar asked Sep 16 '19 13:09

Shubham Pandey


2 Answers

Firebase in-app messaging is triggered by analytical events.

if you want show message in MainActivity ->

class MainActivity : AppCompatActivity() { 
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    ...
    FirebaseAnalytics.getInstance(this).logEvent("main_activity_ready",null)
    //or 
    //FirebaseInAppMessaging.getInstance().triggerEvent("main_activity_ready");

}
}

and select this event in firebase console.

enter image description here

like image 199
Burak Dizlek Avatar answered Oct 17 '22 22:10

Burak Dizlek


Use below method to "Start" and "Stop" InAppMessaging in different Activities.

public static void inAppMessagingInitialization(Context context, boolean setSuppressed,String eventName){ //setSuppressed false means start getting message
    FirebaseInAppMessaging.getInstance().setMessagesSuppressed(setSuppressed); //true==Stop inAppMessaging

    if (!eventName.equals("")){
        FirebaseAnalytics.getInstance(context).logEvent(eventName,null); //To Show InAppMessage in MainActivity. Need to add this event name in Web console campaign
        FirebaseInAppMessaging.getInstance().triggerEvent(eventName);
    }
}

If you don't want show in SplashActivity, then call above method like below:

inAppMessagingInitialization(context,true,""); //Stops inAppMessaging

To show InAppMessage in MainActivity call above method like below and set event name in InAppMessaging console.

inAppMessagingInitialization(context,false,"main_activity_inappmessaging"); //Starts inAppMessaging
like image 20
Zia Avatar answered Oct 17 '22 22:10

Zia