Branch.io:单击链接时深入链接到特定活动

时间:2015-12-16 17:49:27

标签: android deep-linking branch.io

我成功创建了一个深层链接并将其发布到Facebook并附上了所有相关的元数据:

TextView.OnClickListener inviteClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        BranchUniversalObject branchUniversalObject = new BranchUniversalObject()
                // The identifier is what Branch will use to de-dupe the content across many different Universal Objects
                .setCanonicalIdentifier("item/12345")
                        // This is where you define the open graph structure and how the object will appear on Facebook or in a deepview
                .setTitle("Suits")
                .setContentDescription("Great suits here")
                .setContentImageUrl("http://steezo.com/wp-content/uploads/2012/12/man-in-suit.jpg")
                        // You use this to specify whether this content can be discovered publicly - default is public
                .setContentIndexingMode(BranchUniversalObject.CONTENT_INDEX_MODE.PUBLIC)
                        // Here is where you can add custom keys/values to the deep link data
                .addContentMetadata("picurl", "http://steezo.com/wp-content/uploads/2012/12/man-in-suit.jpg");

        LinkProperties linkProperties = new LinkProperties()
                .setChannel("facebook")
                .setFeature("sharing")
                .addControlParameter("$desktop_url", "http://www.yahoo.com")
                .addControlParameter("$ios_url", "http://www.microsoft.com");

        ShareSheetStyle shareSheetStyle = new ShareSheetStyle(PlaceDetailsActivity.this, "Check this out!", "This stuff is awesome: ")
                .setMoreOptionStyle(getResources().getDrawable(android.R.drawable.ic_menu_search), "Show more")
                .addPreferredSharingOption(SharingHelper.SHARE_WITH.FACEBOOK)
                .addPreferredSharingOption(SharingHelper.SHARE_WITH.EMAIL);

        branchUniversalObject.showShareSheet(PlaceDetailsActivity.this,
                linkProperties,
                shareSheetStyle,
                new Branch.BranchLinkShareListener() {
                    @Override
                    public void onShareLinkDialogLaunched() {
                    }
                    @Override
                    public void onShareLinkDialogDismissed() {
                    }
                    @Override
                    public void onLinkShareResponse(String sharedLink, String sharedChannel, BranchError error) {
                        Log.e("LinkShared", "success");
                    }
                    @Override
                    public void onChannelSelected(String channelName) {
                    }
                });

        branchUniversalObject.generateShortUrl(PlaceDetailsActivity.this, linkProperties, new Branch.BranchLinkCreateListener() {
            @Override
            public void onLinkCreate(String url, BranchError error) {
                if (error == null) {
                    Log.i("MyApp", "got my Branch link to share: " + url);
                }
            }
        });
    }
};

我没有成功做的是确保点击链接时,它会转到我应用中的正确活动。我密切关注了指南,但在某些方面我发现指南有点模糊 - https://dev.branch.io/references/android_sdk/#branch-universal-object-for-deep-links-content-analytics-and-indexing

在我想要调用的活动中,我把它放在清单中:

    <activity
        android:name=".SuitActivity"
        android:label=""
        android:windowSoftInputMode="adjustResize">
        <meta-data android:name="io.branch.sdk.auto_link_keys_6" android:value="picurl" />
    </activity>

在SuitActivity类中,我提出了以下内容:

@Override
protected void onResume() {
    super.onResume();
        if (Branch.isAutoDeepLinkLaunch(this)) {
            try {
        action.setPicurl(Branch.getInstance().getLatestReferringParams().getString("picurl"));

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("nondeeplink","Launched by normal application flow");
        }
}

似乎只需要点击Facebook上的链接并打开我的SuitActivity而不是我的MainActivity,但它似乎无法正常工作。当我点击分支链接时,它会打开MainActivity。

当我在Branch上创建链接时,这是日志中返回的内容:

2-16 19:44:38.019 24086-24086/com.example I/MyApp: got my Branch link to share: https://bnc.lt/cByh/7d3u8enomp
12-16 19:44:38.021 24086-24129/com.example I/BranchSDK: posting to https://api.branch.io/v1/url
12-16 19:44:38.021 24086-24129/com.example I/BranchSDK: Post value = {
                                                                                            "identity_id": "20569XXX",
                                                                                            "device_fingerprint_id": "20519XXX",
                                                                                            "session_id": "2057XXX",
                                                                                            "tags": [],
                                                                                            "alias": "",
                                                                                            "channel": "Add to Facebook",
                                                                                            "feature": "sharing",
                                                                                            "stage": "",
                                                                                            "data": "{\"$og_title\":\"Suits\",\"$canonical_identifier\":\"item\\\/12345\",\"$keywords\":[],\"$og_description\":\"Great suits here\",\"$og_image_url\":\"http:\\\/\\\/steezo.com\\\/wp-content\\\/uploads\\\/2012\\\/12\\\/man-in-suit.jpg\",\"$content_type\":\"\",\"$exp_date\":\"0\",\"picurl\":\"http:\\\/\\\/steezo.com\\\/wp-content\\\/uploads\\\/2012\\\/12\\\/man-in-suit.jpg\",\"$desktop_url\":\"http:\\\/\\\/www.yahoo.com\",\"$ios_url\":\"http:\\\/\\\/www.microsoft.com\",\"source\":\"android\"}",
                                                                                            "sdk": "android1.10.1",
                                                                                            "retryNumber": 0,
                                                                                            "branch_key": "key_test_XXX"
                                                                                        }

编辑:

我现在甚至添加了一个GitHub示例供您测试:https://github.com/Winghin2517/BranchIOTestDeepLink

它包括两项活动:

  1. 使用FAB的MainActivity - 它是通过Android Studios创建的新应用的标准模板。如果您点击FAB,Branch.io将启动并要求您添加到Facebook,复制链接等。

  2. SecondActivity - 一旦您在Facebook上分享并点击链接上的Facebook,就应该开始使用SecondActivity。目前,当您点击Facebook上的链接或您共享的链接时,该链接仍会打开MainActivity。

  3. 要使此示例正常运行,请同时将您的branch.IO密钥替换为您在清单中的个人资料中的密钥,目前只有XXX:

    <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_XXX" />
    <meta-data android:name="io.branch.sdk.BranchKey.test" android:value="key_test_XXX" />
    

    谢谢!

1 个答案:

答案 0 :(得分:1)

Github回购现在是一个有关Branch.io的Android工作示例,经过几天与branch.io来回联络。我终于得到了它的工作:

https://github.com/Winghin2517/BranchIOTestDeepLink

如果您点击Facebook中的链接或您决定将链接发送到的位置,该应用将打开&#39; SecondActivity&#39;而不是&#39; MainActivity&#39;如果您点击&#39; SecondActivity&#39;,它将关闭应用,因为{&#39} MainActivity&#39;中将调用onActivityResult方法。使用正确的活动代码,并在&#39; MainActivity&#39;中调用finish()方法。关闭应用程序。