4

我成功创建了一个深层链接并将其发布到 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 -索引

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

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

要使此示例正常工作,请同时将您的 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" />

谢谢!

4

1 回答 1

2

Github repo 现在是一个带有 Branch.io 的 android 的工作示例,经过几天与 branch.io 的来回联系,我终于让它工作了:

https://github.com/Winghin2517/BranchIOTestDeepLink

如果您单击 facebook 中的链接或您决定将链接发送到的任何位置,该应用程序将打开“SecondActivity”,而不是“MainActivity”。如果您从“SecondActivity”单击返回,它将关闭应用程序,因为该onActivityResult方法将在“MainActivity”中使用正确的活动代码调用,并且该finish()方法在“MainActivity”中调用以关闭应用程序。

于 2015-12-18T09:25:17.600 回答