将参数添加到Firebase动态链接 [英] Add parameter to Firebase Dynamic Links
问题描述
我想让用户通过单击按钮并向其他潜在用户发送链接(例如
I want to let the user share a product from my app by clicking a button and sending other potential users links like
www.myapp.com/offer/123
www.myapp.com/offer/123
在用户单击按钮时,必须生成"123",以便以后用来处理它.
there, "123" must be generated at the moment the user click the button in order to, later in time, hanle it with
FirebaseDynamicLinks.getInstance()
.getDynamicLink(getIntent())
.addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
@Override
public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
Uri deepLink;
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.getLink();
但不幸的是,我无法传递参数.
but unfortunetly I am unable to pass a parameter.
String link = "http://www.myapp.com/offer/123";
Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse(link))
.setDynamicLinkDomain("fgd3e.app.goo.gl")
.buildShortDynamicLink()
.addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() {
@Override
public void onComplete(@NonNull Task<ShortDynamicLink> task) {
if (task.isSuccessful()) {
// Short link created
Uri shortLink = task.getResult().getShortLink();
Uri flowchartLink = task.getResult().getPreviewLink();
有人可以教我如何在运行时使用自定义参数创建动态链接,以便将目标用户重新定向到特定的产品详细信息吗?
Can someone teach me how to create a dynamic link at runtime with custom parameters in order to re direct the target user to specific product detail?
推荐答案
SHORT ANSWER:使用查询参数代替路径变量,可以使用以下方法返回的Uri
对象中的getQueryParameter
方法: pendingDynamicLinkData.getLink()
SHORT ANSWER: Using query parameters instead of path variables you could use the getQueryParameter
method from the Uri
object returned by pendingDynamicLinkData.getLink()
我一直在使用查询参数而不是路径变量.
What i've been doing is using query parameters instead of path variables.
而不是发送 http://www.myapp.com/offer/123 ,我正在发送类似 http://www.myapp.com/?offer=123
Instead of sending http://www.myapp.com/offer/123, i'm sending something like http://www.myapp.com/?offer=123
要动态添加参数,我只是串联字符串:"http://www.myapp.com/?offer=" + myValue
To add parameters dynamically i'm just concatenating strings: "http://www.myapp.com/?offer=" + myValue
此URL依次是在Firebase中创建的动态链接的查询参数:
This URL is in turn a query parameter of the dynamic link created in firebase:
String url = "https://YourDynamicLinkIdentifier.app.goo.gl/?link=https://myapp.com?offer="
+ myOfferVar
+ "&apn=com.your.apn"; // << Dont forget to change this too
此生成的URL是我发送给url缩短器的URL.
And this resulting URL is the one i send to the url shortener.
然后在回调onSuccess(PendingDynamicLinkData pendingDynamicLinkData)
中调用pendingDynamicLinkData
的getLink()
,就像您已经在做的那样.
Then in the callback onSuccess(PendingDynamicLinkData pendingDynamicLinkData)
call getLink()
of pendingDynamicLinkData
as you're already doing.
现在有了Uri对象,您可以通过调用方法getQueryParameter("offer")
轻松获得参数.
Now that you have a Uri object, you can easily get the parameter by calling the method getQueryParameter("offer")
.
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.getLink();
String offerKey = deepLink.getQueryParameter("offer");
注意::如果您仍然喜欢使用path变量,则可以获取Uri路径的最后一段.请参阅如何获取uri的最后路径段
NOTE: In case you still prefer to use the path variable, you could get the last segment of the Uri path. See How to obtain the last path segment of an uri
这篇关于将参数添加到Firebase动态链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!