带参数的Firebase动态链接

时间:2019-06-18 17:28:29

标签: java android firebase firebase-dynamic-links

我是社区的新手,由于需要强调我无法解决的问题,所以我加入了。 预先感谢您为我提供的任何帮助。

我目前正在android studio中开发一个项目,并且正在用作firebase数据库,因此我需要共享应用程序中的产品,帖子或列表。因此,我决定使用Firebase Dynamic Link共享某些特定对象。

我当前的代码试图创建一个链接并共享它,我想知道自己在做错什么,因为它创建了链接并让我共享它,但是没有得到我要传递的参数。在我的情况下,“ Lid”是我要通过链接传递的参数,单击链接时,只需选择存储“ Lid”的部分即可。

 holder.BtnShare.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            generateDeepLinkUrl(postCurrent.getLid());

           String url="https://palsuper.page.link/lista_compartida";
            FirebaseDynamicLinks.getInstance().createDynamicLink()
                    .setLink( Uri.parse(url))
                    .setDomainUriPrefix("https://palsuper.page.link")
                    .setAndroidParameters(
                            new DynamicLink.AndroidParameters.Builder("com.ibrahim.palsuper")
                                    .setMinimumVersion(1)
                                    .build())
                    .buildShortDynamicLink( ShortDynamicLink.Suffix.SHORT).addOnCompleteListener( new OnCompleteListener<ShortDynamicLink>() {
                @Override
                public void onComplete(@NonNull Task<ShortDynamicLink> task) {

                    if (task.isSuccessful()) {
                        Uri shortURL = task.getResult().getShortLink();
                        shareDeepLink(shortURL.toString());
                    } else {
                        Toast.makeText(mContext, "error", Toast.LENGTH_SHORT).show();
                    }
                }
            });

            shareDeepLink( url );
        }
    } );


}

private String generateDeepLinkUrl(String Lid) {
    return "https://palsuper.com/lista_compartida=" + Lid ;
}

private void shareDeepLink(String url) {

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, "Hey! check this content out  " + url);
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Check this out !");
    mContext.startActivity(Intent.createChooser(shareIntent, "Share this cool content"));

}

GetLink。

private void getDynamicLink() {
    FirebaseDynamicLinks.getInstance()
            .getDynamicLink(getIntent())
            .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
                @Override
                public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
                    // Get deep link from result (may be null if no link is found)
                    Uri deepLink = null;
                    if (pendingDynamicLinkData != null) {
                        deepLink = pendingDynamicLinkData.getLink();
                        Toast.makeText(HomeActivity.this, "Link obtenido del intent " + deepLink, Toast.LENGTH_SHORT).show();

                       // Log.d(TAG, "Link obtenido del intent " + deepLink.getPath());

                        getAndParseSharedData(deepLink.getPath());


                    }


                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                   // Log.w(TAG, "getDynamicLink:onFailure", e);
                }
            });
}

private void getAndParseSharedData(String url) {

    String pushKey = "";
    if (url.contains("_")) {
        String[] parts = url.split("=");
        pushKey = parts[1];
       // Log.d(TAG, "getAndParseSharedData: " + pushKey);
        Toast.makeText(HomeActivity.this, pushKey, Toast.LENGTH_SHORT).show();
    }

    }

}

2 个答案:

答案 0 :(得分:0)

Joe您需要将要获取的参数作为查询参数传递。

Like this:
"https://palsuper.com?lista_compartida=" + Lid;
                        or
"https://palsuper.com/lista_compartida?lista_compartida=" + Lid;

And then you can fetch it simply using this firebase provided method:

Uri deepLink = null;
                    if (pendingDynamicLinkData != null) {
                        deepLink = pendingDynamicLinkData.getLink();
                        String lista_compartida = deepLink.getQueryParameter("lista_compartida");

}

希望这个答案有帮助。

答案 1 :(得分:0)

转到firbase控制台动态链接部分,然后创建一个动态链接,例如 https://xyz.page.link,然后使用以下代码生成并共享动态链接

 fun generateContentLink(): Uri? {
    val baseUrl = Uri.parse(BASE_URL)
    val domain = "https://xyz.page.link"
    val link = FirebaseDynamicLinks.getInstance()
        .createDynamicLink()
        .setLink(baseUrl)
        .setDomainUriPrefix(domain)
        .setAndroidParameters(AndroidParameters.Builder("com.xyz").build())
        .buildDynamicLink()
    return link.uri
}

private fun onShareClicked() {
    try {
        val link = generateContentLink()
        Log.e("DynamicLnk", "onShareClicked: " + link.toString())
        val subject = " Welcome to QuickPe"
        val msg =
            "Hi,\nInviting you to join xyz\n an interesting app which provides you\n" +
                    "incredible offers on Recharge, Shopping & many more.\n\n" +
                    "Use my referrer code :\n\n " + session()?.getInviteCode().toString() +
                    "\n\nDownload app from link : \n"
        val intent = Intent(Intent.ACTION_SEND)
        intent.type = "text/plain"
        intent.putExtra(Intent.EXTRA_SUBJECT, subject)
        intent.putExtra(Intent.EXTRA_TEXT,
            msg + "\n" + link + "/" + session()?.getInviteCode().toString())
        if (intent.resolveActivity(packageManager) != null) {
            startActivity(intent)
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

现在可以在AndroidManifest.xml中接收以下代码了

 <activity android:name=".xyzActivity">
        <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="https://xyz.page.link"
                android:scheme="https"/>
        </intent-filter>
    </activity>

以及您想接收代码的活动中

 FirebaseDynamicLinks.getInstance()
        .getDynamicLink(intent)
        .addOnSuccessListener(this
        ) { pendingDynamicLinkData ->
            // Get deep link from result (may be null if no link is found)
            var deepLink: Uri? = null
            if (pendingDynamicLinkData != null) {
                deepLink = pendingDynamicLinkData.link
            }
            if (deepLink != null) { //Intent.ACTION_VIEW.equals(appLinkAction) &&
                val code = deepLink.lastPathSegment
                edtReferralCode.setText(code)
            }

            // Handle the deep link. For example, open the linked
            // content, or apply promotional credit to the user's
            // account.
            // ...

            // ...
        }
        .addOnFailureListener(this
        ) { e -> Log.w("=================>", "getDynamicLink:onFailure", e) }