简体   繁体   English

Firebase动态链接打开具体活动数据

[英]Firebase dynamic link to open specific activity with data

I'm facing some problems with implementing Firebase Dynamic links in my app.我在我的应用程序中实现 Firebase 动态链接时遇到了一些问题。

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).一旦用户单击图像,我希望它在该活动(称为 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.如果用户有该应用程序,我希望它打开 BookDetailActivity,其中包含与该书相关的特定意图数据(比如 bookid = abcde),如果用户没有该应用程序,则让他下载该应用程序。

In order to do so, I added to my manifest the following code under BookDetailActivity:为此,我在 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).然后,在 BookDetailActivity 中,我添加了以下内容(使用它只是为了检查应用程序是否在该活动中打开并查看我获得的链接)。

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.我得到的是我成功地将消息发送给另一个用户,用户单击该链接并且应用程序打开但在 MAIN ACTIVITY 而不是 BookDetail 活动。

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 (xylink is just an example):它在我的 Firebase 控制台中的样子是(域(xylink 只是一个例子):

在此处输入图像描述

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?此外,如果用户的 BookDetailActivity 代码中没有FirebaseDynamicLinks部分,那么我的应用程序的早期版本将如何设法获得这些邀请? It will open them to download the app or just do nothing?它会打开它们来下载应用程序还是什么都不做?

Thank you very much非常感谢

I guess, whenever dynamic links are clicked, it opens your MainActivity .我想,只要单击动态链接,它就会打开您的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在您的MainActivity中,使用onNewIntent()方法检查是否由于动态链接而创建了任何新意图

@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: :)希望上面的代码片段对您有所帮助::)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM