美文网首页js css html
Firebase dynamic links 核心的思想和接入之

Firebase dynamic links 核心的思想和接入之

作者: Jam_Chan | 来源:发表于2017-09-05 20:01 被阅读0次

    最近因为项目需求,需要接入firebase dynamic links来增加推广渠道和升级用户体验,刚开始对这个深层动态链接是一脸懵逼的,完全不知道它的核心思想和用法,然后就去官方开发者文档里看了之后,终于大概理解了它的核心思想,官方文档面熟的比较详细:https://firebase.google.com/docs/dynamic-links/,我个人描述就是,跨平台,无论web、iOS或者Android都可以通过动态链接来进入到app的对应的页面(前提是已下载了app,如果没下载,一般是设置跳到应用商店)

    接入步骤:
    1.首先注册并登陆firebase console 控制台,然后创建动态链接

    Paste_Image.png Paste_Image.png

    2.重点一步设置SHA-1 或者SHA-256 (控制台的设置》项目设置》指纹证书)
    由于我的google console 里面设置了SHA-1,如果这里也设置SHA-1会报冲突,建议设置SHA-256


    Paste_Image.png Paste_Image.png

    3.控制台的配置配置好后,剩下的就是代码了
    注入依赖: compile 'com.google.firebase:firebase-invites:11.0.2'
    manifest 里面的配置,记得这个配置在你处理回调的那个类里:

     <activity android:name=".ui.MainActivity">
            <!-- [START link_intent_filter] -->
            <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:host="www.quickgame.com"
                    android:pathPrefix="/index_dynamic_links"
                    android:scheme="https" />
            </intent-filter>
            <!-- [END link_intent_filter] -->
            </activity>
    
    /**
         * firebase dynamic links
         * create and receive
         */
        private void initDynamicLinks() {
            // Create a deep link and display it in the UI
            Uri deepLink = buildDeepLink(Uri.parse(Conf.DEEP_LINK_URL), 0, false);
            // TODO: 2017/9/5 the deepLink should be share
    //        shareDeepLink(deepLink.toString()); // 一般是把这个深层链接分享出去
            // [END_EXCLUDE]
    
            // [START get_deep_link]  // 当用户点击分享出去的深层链接会走这个回调
            FirebaseDynamicLinks.getInstance()
                    .getDynamicLink(getIntent())
                    .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
                        @Override
                        public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
                            // Get deep link from result (may be null if no link is found)
                            Uri deepLink = null;
                            if (pendingDynamicLinkData != null) {
                                deepLink = pendingDynamicLinkData.getLink();
                            }
                            // Handle the deep link. For example, open the linked
                            // content, or apply promotional credit to the user's
                            // account.
                            // ...
    
                            // [START_EXCLUDE]
                            // Display deep link in the UI
                            if (deepLink != null) {
                                // TODO: 2017/9/5 获取回调后的深层链接并做对应处理(跳转或者其他)
                                showLog("deeplink=====" + deepLink.toString());
                            } else {
                                showLog("getDynamicLink: no link found");
                            }
                            // [END_EXCLUDE]
                        }
                    })
                    .addOnFailureListener(this, new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            showLog("getDynamicLink:onFailure" + e);
                        }
                    });
            // [END get_deep_link]
    
     /**
         *创建深层链接,用于分享出去或者其他
         * Build a Firebase Dynamic Link.
         * https://firebase.google.com/docs/dynamic-links/android/create#create-a-dynamic-link-from-parameters
         *
         * @param deepLink   the deep link your app will open. This link must be a valid URL and use the
         *                   HTTP or HTTPS scheme.
         * @param minVersion the {@code versionCode} of the minimum version of your app that can open
         *                   the deep link. If the installed app is an older version, the user is taken
         *                   to the Play store to upgrade the app. Pass 0 if you do not
         *                   require a minimum version.
         * @return a {@link Uri} representing a properly formed deep link.
         */
        @VisibleForTesting
        public Uri buildDeepLink(@NonNull Uri deepLink, int minVersion,  boolean isAd) {
            // Get the unique appcode for this app.
            String appCode = getString(R.string.deeplink_app_code);
    
            // Get this app's package name.
            String packageName = getApplicationContext().getPackageName();
    
            // Build the link with all required parameters
            Uri.Builder builder = new Uri.Builder()
                    .scheme("https")
                    .authority(appCode + ".app.goo.gl")
                    .path("/")
                    .appendQueryParameter("link", deepLink.toString())
                    .appendQueryParameter("apn", packageName);
    
            // If the deep link is used in an advertisement, this value must be set to 1.
            if (isAd) {
                builder.appendQueryParameter("ad", "1");
            }
    
            // Minimum version is optional.
            if (minVersion > 0) {
                builder.appendQueryParameter("amv", Integer.toString(minVersion));
            }
    
            return builder.build();
        }
    
        private void shareDeepLink(String deepLink) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_SUBJECT, "Firebase Deep Link");
            intent.putExtra(Intent.EXTRA_TEXT, deepLink);
            startActivity(intent);
        }
    

    参考的文章:
    https://github.com/firebase/quickstart-android/tree/master/dynamiclinks
    http://www.studyjamscn.com/article-18-1.html

    相关文章

      网友评论

        本文标题:Firebase dynamic links 核心的思想和接入之

        本文链接:https://www.haomeiwen.com/subject/bthpjxtx.html