How to show my app in chooser apps when someone cl

2019-08-21 05:30发布

问题:

How should be my android manifest file if I want to show my app in the chooser apps if someone clicks the dynamic link? The URL prefix is like - https://testapp.page.link. At that stage if someone clicks the link then it first opens the browser then it redirects to my app. But I want to show my app in the chooser app list. At this time my manifest file is like follows-

 <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>

回答1:

We need to write intent-filter in the manifest file as follows-

<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>

Where "testapp.page.link" is the actually the URL prefixes shown at the top left corner of Firebase dynamic link console. And "testapp.com" is the first part of any link. Eg: https://testapp.com/user_profile?id="Zsdsdjwenncsdmsd". From this link, we can extract the user Id as "Zsdsdjwenncsdmsd" at the receiving end of the dynamic link. A complete example is shown below-

If anyone clicks share button this will create the dynamic link-

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);
                            }
                        });

The dynamic link creator function and share function-

 /*-----------------------------------------------------------------------------*/

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);
}

/*---------------------------------------------------------------------------------*/

The String resources are-

R.string.grp_post_link, and 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>

At receiving end we can extract postId, groupKey etc as follows

  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();
        }
    });

The getGroupPost(); is as follows

 private void getGrpPost(String id, String groupKey, final String groupName) {
    //Do what you want
 }