如果用户点击应用程序本身的深层链接会发生什么

时间:2017-09-20 12:41:38

标签: deep-linking branch.io applinks

如果我使用在我的应用中打开特定屏幕的branch.io创建了深层链接。如果我的应用程序中也有此链接,并且用户点击它,它会打开我的屏幕吗?或者它将无效,因为我试图从应用程序打开的链接指向同一个应用程序?

1 个答案:

答案 0 :(得分:1)

当您单击应用程序中webView中的分支链接时,您必须在阅读分支链接参数后处理到特定活动的路由。

这是一个包含webView的示例Activity,并显示了几个Branch链接。单击webView中的链接时,如果单击分支链接,则会重新打开webview并在Toast消息中显示链接参数

public class MainActivity extends AppCompatActivity {

    private WebView webView_;
    private Button button_;
    private String TAG = "WebViewController";
    private Context context_;
    private static final String URL_TO_LOAD = "https://evangelosg.github.io/index.html";
    private static final String BRANCH_LINK_TO_LOAD = "https://ere6.app.link/b6sS0gsCfG";

    @Override
    protected void onNewIntent(Intent intent) {
        Log.d("WebView", "onNewIntent");
        setIntent(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();

        Branch branch = Branch.getInstance();
        branch.initSession(new Branch.BranchReferralInitListener() {
            @Override
            public void onInitFinished(JSONObject referringParams, BranchError error) {
                if (error == null) {
                    Log.d(TAG, referringParams.toString());
                    Toast.makeText(context_, referringParams.toString(), Toast.LENGTH_LONG).show();
                    if (referringParams.has(BundleExtraKeys.CLICKED_BRANCH_LINK)) {
                        try {
                            boolean clickedBranchLink = referringParams.getBoolean(BundleExtraKeys.CLICKED_BRANCH_LINK);
                            if (clickedBranchLink) {
                                //do stuff!
                            }
                        } catch (JSONException e) {
                            Log.d("BranchTrends", e.getMessage());
                        }
                    }
                } else {
                    Log.i("MyApp", error.getMessage());
                }
            }
        }, this.getIntent().getData(), this);
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context_ = this;
        setContentView(R.layout.activity_main);

        webView_ = (WebView) findViewById(R.id.webView);
        webView_.setWebViewClient(new BranchWebViewController("app.link", MainActivity.class));
        webView_.loadUrl(URL_TO_LOAD);

        button_ = (Button) findViewById(R.id.button);
        button_.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
                CustomTabsIntent customTabsIntent = builder.build();
                customTabsIntent.intent.putExtra("branch", BRANCH_LINK_TO_LOAD);
                customTabsIntent.intent.putExtra("branch_force_new_session", true);
                finish();
                customTabsIntent.launchUrl(MainActivity.this, Uri.parse(BRANCH_LINK_TO_LOAD));
            }
        });
    }

    public class BranchWebViewController extends WebViewClient {

        private String myDomain_;
        private Class activityToLaunch_;

        BranchWebViewController(@NonNull String myDomain, Class activityToLaunch) {
            myDomain_ = myDomain;
            activityToLaunch_ = activityToLaunch;
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            String url = request.getUrl().toString();

            if (url.contains(myDomain_)) {
                Intent i = new Intent(view.getContext(), activityToLaunch_);
                i.putExtra("branch", url);
                i.putExtra("branch_force_new_session", true);
                finish();
                startActivity(i);
            } else {
                view.loadUrl(url);
            }

            return true;
        }
    }
} 

阅读链接参数后,您可以根据链接参数路由到相应的活动。