2

Although I have created a Firebase in-app messaging click listener, it tries to open the android system when the button is clicked.

The url like that : https://site_url/product_id

I want to open this url after a logic operation.

class MainActivity : AppCompatActivity() : FirebaseInAppMessagingClickListener {

  override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
      ....

     FirebaseInAppMessaging.getInstance().addClickListener(this)
     FirebaseInAppMessaging.getInstance().triggerEvent("main_activity_ready")
}

  override fun messageClicked(message: InAppMessage, action: Action) {
    val url =( action.actionUrl ?: "")
    Log.d(TAG, "in-app messaging url : $url")
    linkParsePresenter.startLinkParse(url, PreviousPage.InAppMessaging) // This is my logic function.

}
}

enter image description here

messageClicked function invoked. There is no problem. But the system also trying to open this url. How can I override or disable it?

Burak Dizlek
  • 4,805
  • 2
  • 23
  • 19

3 Answers3

3

I'm struggling with this as well.

Unfortunately it looks like there's no way around it at the moment. Firebase will call your custom FirebaseInAppMessagingClickListener, but then it'll try to navigate to the provided action URL regardless.

This is an extract from FirebaseInAppMessagingDisplay :

actionListener = new OnClickListener() {
                    public void onClick(View v) {
                        if (FirebaseInAppMessagingDisplay.this.callbacks != null) {
                            FirebaseInAppMessagingDisplay.this.callbacks.messageClicked(action);
                        }

                        CustomTabsIntent i = (new Builder()).setShowTitle(true).build();
                        i.launchUrl(activity, Uri.parse(action.getActionUrl()));
                        FirebaseInAppMessagingDisplay.this.notifyFiamClick();
                        FirebaseInAppMessagingDisplay.this.removeDisplayedFiam(activity);
                        FirebaseInAppMessagingDisplay.this.inAppMessage = null;
                        FirebaseInAppMessagingDisplay.this.callbacks = null;
                    }
                };

As you can see Firebase will run through all the custom callbacks but then attempt navigation regardless with launchUrl(activity, Uri.parse(action.getActionUrl())).

The only option I can think of is to properly support deep-links in your app as recommended in Customize your Firebase In-App Messaging messages.

1

I solved it by adding a transparent activity which handles the deeplink.

  1. Create empty activity;
class FirebaseEmptyActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        finish()
    }

}
  1. Declare it in manifest:
<activity
   android:name=".FirebaseEmptyActivity"
   android:theme="@style/Theme.Transparent">

   <intent-filter>
      <action android:name="android.intent.action.VIEW" />

      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />

      <data
         android:host="example.com"
         android:pathPrefix="/path-to-access"
         android:scheme="customscheme" />
   </intent-filter>
</activity>

So the above deeplink will work for: customScheme://example.com/path-to-access

Declare a transparent theme:

<style name="Theme.Transparent" parent="AppTheme.NoActionbar">
   <item name="android:windowIsTranslucent">true</item>
   <item name="android:windowBackground">@android:color/transparent</item>
   <item name="android:windowContentOverlay">@null</item>
   <item name="android:windowNoTitle">true</item>
   <item name="android:windowIsFloating">false</item>
   <item name="android:backgroundDimEnabled">false</item>
</style>

Now in your activity listen for the button click:

FirebaseInAppMessaging.getInstance().removeAllListeners()
FirebaseInAppMessaging.getInstance().addClickListener { inAppMessage, action -> 
   if (action.actionUrl?.contains("your custom scheme url...") == true) {
      // The button was clicked for that action...
   }
}
Zbarcea Christian
  • 9,367
  • 22
  • 84
  • 137
1

Firebase In-App Messaging (FIAM) is in Beta and doesn't support actions other than dynamic links as of now. And dynamic links are still links, so it will always try to navigate to the web first. Jumps outside the app then back (which makes it look like a virus)

I think a better alternative is to use Firebase Cloud Messaging instead. The implementation looks more complex, but I highly recommend watching this guy explaining everything in detail: https://www.youtube.com/watch?v=p7aIZ3aEi2w

Benefits of Firebase Cloud Messaging over Firebase In-App Messaging:

  • Push notifications - if the goal with FIAM was to communicate with users, this gets it done
  • Custom data - A key/value payload can be sent to the app, so you can customize the click action handler, better than FIAM, where it always opens the dynamic link
  • Silent messages - if you really want to replicate FIAM without the notifications part, you can send silent messages and handle the payload with custom dialogs in the app (silent messages can only be sent from the server, not through the Firebase Console yet)
  • FCM can be A/B tested as the FIAM

Hope that helps and hope that Firebase makes FIAM more user-friendly in the future (like with a custom callback to handle the button's click)

Kallos Zsolty
  • 211
  • 3
  • 5