如果有人点击动态链接,如果我想在选择器应用程序中显示我的应用程序,我的Android清单文件应该如何? URL前缀类似于 - https://testapp.page.link。在那个阶段,如果有人点击链接,那么它首先打开浏览器然后重定向到我的应用程序。但我想在选择器应用列表中显示我的应用。这时我的清单文件如下 -
<activity android:name=".extraActivities.DynamicLinkActivity">
<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="test.com"
android:pathPattern="https://testapp.page.link*"
android:scheme="https"
/>
</intent-filter>
</activity>
我们需要在清单文件中编写intent-filter,如下所示 -
<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="testapp.com"
android:pathPattern=".*"
android:scheme="https" />
<data
android:host="testapp.page.link"
android:scheme="https"
android:pathPattern=".*"/>
</intent-filter>
其中“testapp.page.link”实际上是Firebase动态链接控制台左上角显示的URL前缀。而“testapp.com”是任何链接的第一部分。例如:https://testapp.com/user_profile?id=“Zsdsdjwenncsdmsd”。通过此链接,我们可以在动态链接的接收端将用户ID提取为“Zsdsdjwenncsdmsd”。一个完整的例子如下所示 -
如果有人点击分享按钮,则会创建动态链接 -
shareBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shareProgressBar.setVisibility(View.VISIBLE);
Task<ShortDynamicLink> shortDynamicLinkTask = buildDeepLink(getString(R.string.grp_post_link)+postsModel.getPostId()+"&groupKey="+postsModel.getGroupKey()+"&groupName="+ dataSnapshot.getValue(String.class));
shortDynamicLinkTask.addOnCompleteListener(new OnCompleteListener<ShortDynamicLink>() {
@Override
public void onComplete(@NonNull Task<ShortDynamicLink> task) {
grpPostsViewHolder.shareProgressBar.setVisibility(View.GONE);
if(task.isSuccessful()){
Uri uri = task.getResult().getShortLink();
share(dataSnapshot.getValue(String.class), uri.toString());
}else {
Toast.makeText(context, "Can't create link", Toast.LENGTH_SHORT).show();
}
}
});
shortDynamicLinkTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
shareProgressBar.setVisibility(View.GONE);
}
});
动态链接创建者功能和共享功能 -
/*-----------------------------------------------------------------------------*/
private Task<ShortDynamicLink> buildDeepLink(String deepLink) {
String uriPrefix = getString(R.string.dynamic_links_uri_prefix);
return FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse(deepLink))
.setDomainUriPrefix(uriPrefix)
.setNavigationInfoParameters(new DynamicLink.NavigationInfoParameters.Builder()
.build())
.setAndroidParameters(new DynamicLink.AndroidParameters.Builder()
.setMinimumVersion(3)
.build())
.buildShortDynamicLink();
}
/*-----------------------------------------------------------------------------*/
private void share(String name, String deepLink) {
String message = "Find "+name+" on SelfieLe - link: "+deepLink;
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(intent);
}
/*---------------------------------------------------------------------------------*/
String资源是 -
R.string.grp_post_link和R.string.dynamic_links_uri_prefix:
<string name="user_profile_link">https://testapp.com/user_profile?id=</string>
<string name="dynamic_links_uri_prefix">https://testapp.page.link</string>
在接收端,我们可以如下提取postId,groupKey等
FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent()).addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
@Override
public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.getLink();
}
if (deepLink != null) {
getGrpPost(deepLink.getQueryParameter("id"), deepLink.getQueryParameter("groupKey"), deepLink.getQueryParameter("groupName"));
}else {
Toast.makeText(DynamicLinkActivity.this, "Can't find link", Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(DynamicLinkActivity.this, "Can't find link", Toast.LENGTH_SHORT).show();
}
});
getGroupPost();如下
private void getGrpPost(String id, String groupKey, final String groupName) {
//Do what you want
}