Firebase dynamic link to open specific activity with data

Viewed 617

I'm facing some problems with implementing Firebase Dynamic links in my app.

My app contains a gallery of books and under each book, there is a share button. When a user clicks that button it creates a message that he can send other uses saying something like: "Hey you might like this book, check it here!"

Once the user clicks the image, I had like it to open the app at that activity (called BookDetailActivity). If the user has the app, I had like it to open BookDetailActivity with the specific intent data that is relevant to that book (say bookid = abcde) and if the user doesn't have the app, to send him to download the app.

In order to do so, I added to my manifest the following code under BookDetailActivity:

<activity android:name=".BookDetailActivity">
    <intent-filter android:label="SB">
        <action android:name="android.intent.action.VIEW" />

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

        <data
            android:host="https://x.y.link"
            android:pathPrefix="/SB"
            android:scheme="https" />
    </intent-filter>
</activity>

Then, inside the BookDetailActivity, I added the following (used it just to check if the app opens at that activity and to see the link I get).

FirebaseDynamicLinks.getInstance()
        .getDynamicLink( getIntent() )
        .addOnSuccessListener( this, new OnSuccessListener<PendingDynamicLinkData>() {
            @Override
            public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
                Uri deepLink = null;
                if (pendingDynamicLinkData != null) {
                    deepLink = pendingDynamicLinkData.getLink();
                    Log.d( "TEST1", deepLink + "" );
                }
            }
        } )
        .addOnFailureListener( this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d( "TEST2", "HERE" );
            }
        } );

Now, the button that sends that invitation message is:

ib_Share.setOnClickListener( v -> {
    Intent shareIntent = new Intent();
    shareIntent.setAction( Intent.ACTION_SEND );

    String suggest = "Hey you might like this book, check it here! - https://x.y.link/SB?bookid=abcde";
    shareIntent.putExtra( Intent.EXTRA_TEXT, Html.fromHtml( suggest ) );
    shareIntent.setType( "text/html" );
    startActivity( shareIntent );
} );

What I get is that I successfully send the message to another user, the user clicks on that link and the app opens but at the MAIN ACTIVITY instead of at BookDetail activity.

Is there a reason it won't take the user to that correct activity?

The way it looks in my Firebase Console is( the domain (x.y.link is just an example):

enter image description here

Also, how users that have a previous version of my app without the FirebaseDynamicLinks part in their BookDetailActivity code will manage to get these invitations? It will open them to download the app or just do nothing?

Thank you very much

1 Answers

I guess, whenever dynamic links are clicked, it opens your MainActivity. I tried to arrive at the solution as below.

In your MainActivity, check if there is any new intent created because of the dynamic link using onNewIntent() method

@Override
protected void onNewIntent ( Intent newIntent ) {
   super.onNewIntent ( newIntent );
   setIntent ( newIntent );
   checkDynamicLinkIntent ();
}

private void checkDynamicLinkIntent () {
   if (!Objects.equals ( null, getIntent ().getData () )) {
       Uri dynamicLink = Uri.parse ( getIntent ().getData ().toString () );
       parameterValue = dynamicLink.getQueryParameter ( parameterName );
       //get any data from your URI if needed
       Intent intent = new Intent ( MainActivity.this, BookDetailActivity.class );
       intent.putExtra ( "bookDetail", parameterValue );
       startActivity ( intent );
    }
}

Hope the above code snippet helps you! :)

Related