Search code examples
androidfacebook-android-sdkfacebook-share

how to implement Facebook sharing and then deep link that post using deep link(open my app) in android App?


I want to implement deep linking for facebook app post. Firstly I want to share my App content on Facebook Post and when user tap on the post then if User already has the app installed then open app otherwise It will open app link.

I follow https://developers.facebook.com/docs/applinks/android and https://developers.facebook.com/docs/sharing/android#linkshare but It's not working

how to share this data using LinkShare on facebook

target_url: "https://developers.facebook.com/android" extras: fb_app_id: [YOUR_FACEBOOK_APP_ID] fb_access_token: "[ACCESS_TOKEN]" fb_expires_in: 3600


Solution

  • To implement deep linking and sharing together, need to implement this feature using branch.io

    Add dependency :

     compile 'com.google.android.gms:play-services-appindexing:9.+' 
    

    Add this code in the manifest file inside Launcher Activity

      <!-- Branch URI Scheme -->
            <intent-filter>
                <data android:scheme="androidexample" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
    
            <!-- Branch App Links (optional) -->
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="https" android:host="example.app.link" />
                <data android:scheme="https" android:host="example-alternate.app.link" />
            </intent-filter>    
    

    Add this code to your Launcher Activity, You will get your link and data in this method

    @Override
    public void onStart() {
        super.onStart();
    
        // Branch init
        Branch.getInstance().initSession(new Branch.BranchReferralInitListener() {
            @Override
            public void onInitFinished(JSONObject referringParams, BranchError error) {
                if (error == null) {
                    Log.i("BRANCH SDK", referringParams.toString());
                    // Retrieve deeplink keys from 'referringParams' and evaluate the values to determine where to route the user
                    // Check '+clicked_branch_link' before deciding whether to use your Branch routing logic
                } else {
                    Log.i("BRANCH SDK", error.getMessage());
                }
            }
        }, this.getIntent().getData(), this);
    }    
    

    Add this code in MyApplication class

     // Branch logging for debugging
        Branch.enableLogging();
    
     // Branch object initialization
        Branch.getAutoInstance(this);   
    

    You can create deep link using this code

     LinkProperties lp = new LinkProperties()
    .setChannel("facebook")
    .setFeature("sharing")
    .setCampaign("content 123 launch")
    .setStage("new user")
    .addControlParameter("$desktop_url", "http://example.com/home")
    .addControlParameter("custom", "data")
    .addControlParameter("custom_random", 
    Long.toString(Calendar.getInstance().getTimeInMillis()));
    
    buo.generateShortUrl(this, lp, new 
    Branch.BranchLinkCreateListener() {
    @Override
    public void onLinkCreate(String url, BranchError error) {
        if (error == null) {
            Log.i("BRANCH SDK", "got my Branch link to share: " + url);
        }
    }
    });    
    

    refer this for Android

    refer this for ios