Android Branch deferred deep link 深度链接

Branch 实现Android端 deferred deep link

官网sdk文档地址:https://docs.branch.io/apps/android/
1. Configure Branch(配置Branch)
Android Branch deferred deep link 深度链接_第1张图片
Android Branch deferred deep link 深度链接_第2张图片
2. Install Branch

  • Import the Branch SDK to your build.gradle
    // required for all Android apps
    implementation 'io.branch.sdk.android:library:3.+'

    // required if your app is in the Google Play Store
    implementation 'com.google.android.gms:play-services-appindexing:9.+' // App indexing
    implementation 'com.google.android.gms:play-services-ads:9+' // GAID matching

3.Configure app

  • Add Branch to your AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eneff.branch.example.android">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:name="com.eneff.branch.example.android.CustomApplicationClass"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!-- Launcher Activity to handle incoming Branch intents -->
        <activity
            android:name=".LauncherActivity"
            android:launchMode="singleTask"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

		<activity android:name=".intelsports.ShareShowActivity">
	        <!-- 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="olp" android:host="sports.app.link" />
	        </intent-filter>
	    </activity>

        <!-- Branch init -->
        <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Sw" />

        <!-- Branch install referrer tracking (optional) -->
        <receiver android:name="io.branch.referral.InstallListener" android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

  • Replace the following with values from your Branch Dashboard App settings and Link settings

    example.app.link
    key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Sw

    Android Branch deferred deep link 深度链接_第3张图片
    Android Branch deferred deep link 深度链接_第4张图片Android Branch deferred deep link 深度链接_第5张图片
    以上是Branch的配置信息设置,接下来进行Branch的初始化工作

4.Initialize Branch

  • Add Branch to your LauncherActivity.java
public class LauncherActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
    }

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

    @Override
    public void onNewIntent(Intent intent) {
        this.setIntent(intent);
    }
}

5.Load Branch

  • Add Branch to your CustomApplicationClass.java
public class CustomApplicationClass extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        // Branch logging for debugging
        Branch.enableDebugMode();

        // Branch object initialization
        Branch.getAutoInstance(this);
    }
}

6.Test deep link

BranchUniversalObject buo = new BranchUniversalObject();
    
LinkProperties lp = new LinkProperties()
    .addControlParameter("custom1", "data1")    //传入自定义数据
    .addControlParameter("custom2", "data2");

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

使用第三方分享或系统自带分享工具,将以上短链接url分享即可。

7.Read deep link(读取depplink中携带的数据)

  • 在要启动的ShareShowActivity或LauncherActivity onStart方法中实现。即可读取出JSONObject中相应数据
// listener (within Main Activity's onStart)
Branch.getInstance().initSession(new Branch.BranchReferralInitListener() {
    @Override
    public void onInitFinished(JSONObject referringParams, BranchError error) {
        if (error == null) {
            Log.i("BRANCH SDK", referringParams.toString());
        } else {
            Log.i("BRANCH SDK", error.getMessage());
        }
    }
}, this.getIntent().getData(), this);

// latest
JSONObject sessionParams = Branch.getInstance().getLatestReferringParams();

// first 若设备未安装app,用户安装app后第一次启动即可获取deep link中的数据
JSONObject installParams = Branch.getInstance().getFirstReferringParams();

你可能感兴趣的:(Android代码)