最近因?yàn)轫?xiàng)目需求,需要接入firebase dynamic links來(lái)增加推廣渠道和升級(jí)用戶體驗(yàn),剛開(kāi)始對(duì)這個(gè)深層動(dòng)態(tài)鏈接是一臉懵逼的,完全不知道它的核心思想和用法,然后就去官方開(kāi)發(fā)者文檔里看了之后,終于大概理解了它的核心思想,官方文檔面熟的比較詳細(xì):https://firebase.google.com/docs/dynamic-links/,我個(gè)人描述就是,跨平臺(tái),無(wú)論web、iOS或者Android都可以通過(guò)動(dòng)態(tài)鏈接來(lái)進(jìn)入到app的對(duì)應(yīng)的頁(yè)面(前提是已下載了app,如果沒(méi)下載,一般是設(shè)置跳到應(yīng)用商店)
接入步驟:
1.首先注冊(cè)并登陸firebase console 控制臺(tái),然后創(chuàng)建動(dòng)態(tài)鏈接


2.重點(diǎn)一步設(shè)置SHA-1 或者SHA-256 (控制臺(tái)的設(shè)置》項(xiàng)目設(shè)置》指紋證書(shū))
由于我的google console 里面設(shè)置了SHA-1,如果這里也設(shè)置SHA-1會(huì)報(bào)沖突,建議設(shè)置SHA-256


3.控制臺(tái)的配置配置好后,剩下的就是代碼了
注入依賴: compile 'com.google.firebase:firebase-invites:11.0.2'
manifest 里面的配置,記得這個(gè)配置在你處理回調(diào)的那個(gè)類(lèi)里:
<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()); // 一般是把這個(gè)深層鏈接分享出去
// [END_EXCLUDE]
// [START get_deep_link] // 當(dāng)用戶點(diǎn)擊分享出去的深層鏈接會(huì)走這個(gè)回調(diào)
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 獲取回調(diào)后的深層鏈接并做對(duì)應(yīng)處理(跳轉(zhuǎn)或者其他)
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]
/**
*創(chuàng)建深層鏈接,用于分享出去或者其他
* 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