Android:使用 Branch.io 生成深层链接和 Google Invites 进行内容共享

标签 android google-app-invites branch.io

我正在阅读 Google 邀请:https://developers.google.com/app-invites/android/guides/app还有 branch.io:https://dev.branch.io/recipes/content_sharing/android/#routing-to-content-within-your-android-app

Google Invites 似乎擅长内容共享,它提供了一个界面,可以从 Google 中选择所有你想发送深层链接到你的应用程序的人。

Branch.io 似乎擅长生成深层链接,他们的 shorturl 将在“嵌入键/值深层链接元数据”中包含应用程序所需的所有数据。

Branch.io 也有一个内置的“本地共享表”,但我认为它不如 Google Invites 先进/好。

我想将 Branch.io 深层链接与 Google Invites 界面一起用于内容共享。

我正在努力解决的问题是将两者合并。

当有人点击 Google Invites 链接时,Android 应用程序打开并在 onCreate 方法内部运行以下代码来拦截 Intent :

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...

    if (savedInstanceState == null) {
        // No savedInstanceState, so it is the first launch of this activity
        Intent intent = getIntent();
        if (AppInviteReferral.hasReferral(intent)) {
            // In this case the referral data is in the intent launching the MainActivity,
            // which means this user already had the app installed. We do not have to
            // register the Broadcast Receiver to listen for Play Store Install information
            launchDeepLinkActivity(intent);
        }
    }
}

Branch.io 告诉 android 在 onStart 方法中拦截 Intent :

@Override
public void onStart() {
    super.onStart();

    Branch branch = Branch.getInstance();

    // If NOT using automatic session management
    // Branch branch = Branch.getInstance(getApplicationContext());

    branch.initSession(new BranchReferralInitListener(){
        @Override
        public void onInitFinished(JSONObject referringParams, Branch.BranchError error) {
            if (error == null) {
                // params are the deep linked params associated with the link that the user clicked before showing up
                // params will be empty if no data found
                String pictureID = referringParams.optString("picture_id", "");
                if (pictureID.equals("")) {
                    startActivity(new Intent(this, HomeActivity.class));
                }
                else {
                    Intent i = new Intent(this, ViewerActivity.class);
                    i.putExtra("picture_id", pictureID);
                    startActivity(i);
                }
            } else {
                Log.e("MyApp", error.getMessage());
            }
        }
    }, this.getIntent().getData(), this);
}

如果我同时使用 Branch.io 和 Google Invites,我应该使用哪个代码来拦截在我点击深层链接时启动 Android 应用程序的 Intent ?

最佳答案

我将分支 SDK 放入谷歌的应用程序邀请示例中并尝试了一些东西。似乎您无法将分支 URL 作为深层链接提供给 AppInviteInvitation IntentBuilder,除非链接被转换为 google 深层链接。您可以将消息本身设置为分支 URL,setMessage(url),并可能为深层链接提供 NULL URI,但这有点老套。我自己试过了,电子邮件功能只用了一半,但发短信没有用。

在没有分享表的情况下,默认生成和分享分支链接的方式如下:

BranchShortLinkBuilder shortUrlBuilder = new BranchShortLinkBuilder(MainActivity.this)
        .addTag("tag1")
        .addTag("tag2")
        .setChannel("channel1")
        .setFeature("feature1")
        .setStage("1")
        .addParameters("name", "test name") // deeplink data - anything you want!
        .addParameters("message", "hello there with short url")
        .addParameters("$og_title", "this is a title")
        .addParameters("$og_description", "this is a description")
        .addParameters("$og_image_url", "https://imgurl.com/img.png");

// Get URL Asynchronously
shortUrlBuilder.generateShortUrl(new Branch.BranchLinkCreateListener() {
    @Override
    public void onLinkCreate(String url, BranchError error) {
        if (error != null) {
            Log.e("Branch Error", "Branch create short url failed. Caused by -" + error.getMessage());
        } else {
            Log.i("Branch", "Got a Branch URL " + url);

            Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
                    .setMessage(url)
                    .setDeepLink(Uri.parse(url))
                    .setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
                    .build();
            startActivityForResult(intent, REQUEST_INVITE);


        }
    }
});

至于你应该使用哪个来拦截 Intent :使用 Branch.initSession 用于 Branch 链接,使用 AppInviteReferral.hasReferral(intent) 用于谷歌链接。为了更好的共享对话而将服务交织在一起并不是我要采取的方向。相反,使用带有自定义共享对话框的分支链接(它们通过安装传递数据的速度比 Android SDK 更快),有很多方法可以构建自定义对话框,谷歌搜索/查看 github。

如果您有任何其他问题或需要更多信息,请告诉我,我很乐意提供帮助。

关于Android:使用 Branch.io 生成深层链接和 Google Invites 进行内容共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33299806/

相关文章:

ios - 未安装应用程序时检索深层链接 (iOS)

ios - Branch.io 统计数据

android - 由于branch.io,应用程序已从 Google Play 中删除

Android 深度链接 - 使用 Branch.io 链接未在 Android 中打开

android - 自定义 EditText 的 setError 图标

android - 左侧和右侧的 TableLayout 项目

javascript - 当移动地址栏出现/消失时防止窗口高度变化

android - 在 setVisible 设置为 false 然后 true 之后,ActionBar 图标不再出现在 ActionBar 上

android - 从 AppInvite Google android 获取结果

android - 您在哪里提交 Android/iOS Google 产品或服务(AppInvites、 map 等)的错误?