5

I'm successful in creating a deeplink and posting it to facebook with all the relevant metadata attached:

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

What I'm not successful at doing is to ensure that when the link is clicked, it goes to the correct activity within my app. I followed the guide closely but at points I found the guide to be a bit vague - https://dev.branch.io/references/android_sdk/#branch-universal-object-for-deep-links-content-analytics-and-indexing.

In the activity that I want to call, I put this in the manifest:

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

Inside the SuitActivity class, I put in the following:

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

That appeared to be all that needs to be done to click on a link in Facebook and open my SuitActivity instead of my MainActivity but it doesn't seem to work. When I click on a branch link, it opens MainActivity.

When I create the link on Branch, this is what is returned in the logs:

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

EDIT:

I have even added a GitHub sample now for you to test this: https://github.com/Winghin2517/BranchIOTestDeepLink

It includes two activities:

  1. MainActivity with a FAB - it is the standard template for a new app created through Android Studios. If you click on the FAB, Branch.io will launch and ask you to add to facebook, copy link etc.

  2. SecondActivity - once you have shared it on facebook and click on facebook on the link, the SecondActivity should start. Currently, when you click on the link on facebook or everywhere you have shared it, the link still opens MainActivity.

For this sample to work, please also replace your branch.IO key with your key from your profile in the manifest as currently, they are just XXX's:

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

Thanks!

Simon
  • 19,658
  • 27
  • 149
  • 217
  • I have no experience with deep linking, but I noticed that a library that I use supports creating them https://github.com/BoltsFramework/Bolts-Android/blob/master/README.md#app-links, thought it might be helpful to you – cYrixmorten Dec 16 '15 at 17:59
  • On a second look seems like that is what branch.io is supposed to be able to do. Nevermind then :-) – cYrixmorten Dec 16 '15 at 18:01
  • Yes - branch is used for deeplinking content but i'm just struggling with it. I have also contacted branch.io and reference this SO post on email so hopefully someone from there will take a look. – Simon Dec 16 '15 at 18:06

1 Answers1

2

The Github repo is now a working example of android with Branch.io after several days liaising back and forth with branch.io I finally got it to work:

https://github.com/Winghin2517/BranchIOTestDeepLink

The app will open the 'SecondActivity', and not the 'MainActivity', if you click on the link in facebook or wherever you decided to send the link to. If you click back from the 'SecondActivity', it will close the app as the onActivityResult method will be called in the 'MainActivity' with the correct activity code and the finish() method is called in 'MainActivity' to close the app.

Simon
  • 19,658
  • 27
  • 149
  • 217