Firebase 动态链接无法获取参数
Firebase Dynamic Links failing to fetch parameter
我正在开发我的应用程序中的邀请,我希望用户推荐其他人,推荐人在他们推荐的人注册后将获得奖励。我正在关注这个 firebase invite tutorial,在 步骤 2 中,我们使用我们的参数 invitedby=SENDER_UID
从我们的正常 link 创建了一个动态 link 像
String link = "https://samplelink.com/?invitedby=" + user.getPhone();
Log.e(TAG, "Generated link: "+link);
FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse(link))
.setDynamicLinkDomain(getString(R.string.dynamic_link_domain))
.setAndroidParameters(
new DynamicLink.AndroidParameters.Builder(getPackageName())
.build())
.setIosParameters(
new DynamicLink.IosParameters.Builder("com.example.ios")
//.setAppStoreId("123456789")
// .setMinimumVersion("1.0.1")
.build())
.buildShortDynamicLink()
.addOnSuccessListener(shortDynamicLink -> {
Uri mInvitationUrl = shortDynamicLink.getShortLink();
Log.e(TAG, "generated URL: "+mInvitationUrl.toString());
Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invite_and_earn))
.setMessage(getString(R.string.invitation_message))
.setDeepLink(mInvitationUrl)
// .setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
// .setCallToActionText(user.getPhone())
.build();
startActivityForResult(intent, REQUEST_INVITE);
}).addOnFailureListener(e -> Log.e(TAG, "Error Generating Deeplink: "+e.getLocalizedMessage()));
});
并发送生成的short dynamic link
。当用户在 第 4 步 打开应用程序时,我们得到了 link 喜欢
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();
}
//
// If the user isn't signed in and the pending Dynamic Link is
// an invitation, sign in the user anonymously, and record the
// referrer's UID.
//
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user == null
&& deepLink != null
&& deepLink.getBooleanQueryParameter("invitedby")) {
String referrerUid = deepLink.getQueryParameter("invitedby");
createAnonymousAccountWithReferrerInfo(referrerUid);
}
}
});
link shorted dynamic link
生成正常,并发送,如果在浏览器中打开原始 https://samplelink.com/?invitedby=" + user.getPhone()
显示但在应用程序中我只得到 shortdynamiclink
所以从未找到 invitedby。有人遇到过这个问题吗?
我的样本 short dynamic link
您可以使用 appendQueryParameter()
而不是将参数与 link 连接
String link = "https://samplelink.com/";
Log.e(TAG, "Generated link: "+link);
FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse(link).buildUpon().appendQueryParameter("invitedby",user.getPhone()).build())
.setDynamicLinkDomain(getString(R.string.dynamic_link_domain))
.setAndroidParameters(
new DynamicLink.AndroidParameters.Builder(getPackageName())
.build())
.setIosParameters(
new DynamicLink.IosParameters.Builder("com.example.ios")
//.setAppStoreId("123456789")
// .setMinimumVersion("1.0.1")
.build())
并且在你的 activity 上可以检索你的参数如下,
FirebaseDynamicLinks.getInstance().getDynamicLink(getActivity().getIntent())
.addOnSuccessListener(getActivity(), new OnSuccessListener<PendingDynamicLinkData>() {
@Override
public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
Uri deepLink = null;
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.getLink();
}
final Uri finalDeepLink = deepLink;
if (finalDeepLink != null && finalDeepLink.getBooleanQueryParameter("invitedby", false)) {
String referrerPhoneId = finalDeepLink.getQueryParameter("invitedby");
我最终使用 Invite
API 而不是
Uri uri = Uri.parse(getString(R.string.invitation_deep_link));
Uri deepLinkPlus = Uri.withAppendedPath(uri, user.getPhone());
Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invite_and_earn))
.setMessage(getString(R.string.invitation_message))
.setDeepLink(deepLinkPlus)
//.setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
//.setCallToActionText("invitedby")
.build();
startActivityForResult(intent, REQUEST_INVITE);
并从接收 deeplink
的 activity
FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent())
.addOnSuccessListener(this, data -> {
if (data == null) {
Log.d(TAG, "getInvitation: no data");
return;
}
Uri deepLink = data.getLink();
String referrerPhone = deepLink.getLastPathSegment();
SharedPreferencesUtils.saveString(this, SharedPreferencesUtils.PREF_REFERER, referrerPhone);
})
.addOnFailureListener(this, e -> Log.w(TAG, "getDynamicLink:onFailure", e));
我正在开发我的应用程序中的邀请,我希望用户推荐其他人,推荐人在他们推荐的人注册后将获得奖励。我正在关注这个 firebase invite tutorial,在 步骤 2 中,我们使用我们的参数 invitedby=SENDER_UID
从我们的正常 link 创建了一个动态 link 像
String link = "https://samplelink.com/?invitedby=" + user.getPhone();
Log.e(TAG, "Generated link: "+link);
FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse(link))
.setDynamicLinkDomain(getString(R.string.dynamic_link_domain))
.setAndroidParameters(
new DynamicLink.AndroidParameters.Builder(getPackageName())
.build())
.setIosParameters(
new DynamicLink.IosParameters.Builder("com.example.ios")
//.setAppStoreId("123456789")
// .setMinimumVersion("1.0.1")
.build())
.buildShortDynamicLink()
.addOnSuccessListener(shortDynamicLink -> {
Uri mInvitationUrl = shortDynamicLink.getShortLink();
Log.e(TAG, "generated URL: "+mInvitationUrl.toString());
Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invite_and_earn))
.setMessage(getString(R.string.invitation_message))
.setDeepLink(mInvitationUrl)
// .setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
// .setCallToActionText(user.getPhone())
.build();
startActivityForResult(intent, REQUEST_INVITE);
}).addOnFailureListener(e -> Log.e(TAG, "Error Generating Deeplink: "+e.getLocalizedMessage()));
});
并发送生成的short dynamic link
。当用户在 第 4 步 打开应用程序时,我们得到了 link 喜欢
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();
}
//
// If the user isn't signed in and the pending Dynamic Link is
// an invitation, sign in the user anonymously, and record the
// referrer's UID.
//
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user == null
&& deepLink != null
&& deepLink.getBooleanQueryParameter("invitedby")) {
String referrerUid = deepLink.getQueryParameter("invitedby");
createAnonymousAccountWithReferrerInfo(referrerUid);
}
}
});
link shorted dynamic link
生成正常,并发送,如果在浏览器中打开原始 https://samplelink.com/?invitedby=" + user.getPhone()
显示但在应用程序中我只得到 shortdynamiclink
所以从未找到 invitedby。有人遇到过这个问题吗?
我的样本 short dynamic link
您可以使用 appendQueryParameter()
String link = "https://samplelink.com/";
Log.e(TAG, "Generated link: "+link);
FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse(link).buildUpon().appendQueryParameter("invitedby",user.getPhone()).build())
.setDynamicLinkDomain(getString(R.string.dynamic_link_domain))
.setAndroidParameters(
new DynamicLink.AndroidParameters.Builder(getPackageName())
.build())
.setIosParameters(
new DynamicLink.IosParameters.Builder("com.example.ios")
//.setAppStoreId("123456789")
// .setMinimumVersion("1.0.1")
.build())
并且在你的 activity 上可以检索你的参数如下,
FirebaseDynamicLinks.getInstance().getDynamicLink(getActivity().getIntent())
.addOnSuccessListener(getActivity(), new OnSuccessListener<PendingDynamicLinkData>() {
@Override
public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
Uri deepLink = null;
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.getLink();
}
final Uri finalDeepLink = deepLink;
if (finalDeepLink != null && finalDeepLink.getBooleanQueryParameter("invitedby", false)) {
String referrerPhoneId = finalDeepLink.getQueryParameter("invitedby");
我最终使用 Invite
API 而不是
Uri uri = Uri.parse(getString(R.string.invitation_deep_link));
Uri deepLinkPlus = Uri.withAppendedPath(uri, user.getPhone());
Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invite_and_earn))
.setMessage(getString(R.string.invitation_message))
.setDeepLink(deepLinkPlus)
//.setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
//.setCallToActionText("invitedby")
.build();
startActivityForResult(intent, REQUEST_INVITE);
并从接收 deeplink
FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent())
.addOnSuccessListener(this, data -> {
if (data == null) {
Log.d(TAG, "getInvitation: no data");
return;
}
Uri deepLink = data.getLink();
String referrerPhone = deepLink.getLastPathSegment();
SharedPreferencesUtils.saveString(this, SharedPreferencesUtils.PREF_REFERER, referrerPhone);
})
.addOnFailureListener(this, e -> Log.w(TAG, "getDynamicLink:onFailure", e));