Firebase 深度 Link 简短 URL
Firebase Deep Link short URL
可以缩短 Firebase 深层链接的链接吗?他们有这个功能吗?生成的链接太长,这样不好。
可以在 Firebase 控制台的“动态链接”选项卡中缩短 link。点击 'New Dynamic Link',您可以选择从现有的 link.
创建短 link
目前无法以编程方式完成此操作。
更新
Firebase 现在支持缩短动态 links programmatically.
在以编程方式创建动态 link 时,我遇到了同样的问题,变得冗长且用户不友好 URL。
我找到的解决方案是使用效果很好的 Google URL Shortener API。
link 指向 Java 库,我在 Android 中使用它,但你也可以做一个简单的 http 请求。
我会 post 我的 Android 代码以备不时之需:
private void createDynamicLink() {
// 1. Create the dynamic link as usual
String packageName = getApplicationContext().getPackageName();
String deepLink = "YOUR DEEPLINK";
Uri.Builder builder = new Uri.Builder()
.scheme("https")
.authority(YOUR_DL_DOMAIN)
.path("/")
.appendQueryParameter("link", deepLink)
.appendQueryParameter("apn", packageName);
final Uri uri = builder.build();
//2. Create a shorten URL from the dynamic link created.
Urlshortener.Builder builderShortener = new Urlshortener.Builder (AndroidHttp.newCompatibleTransport(), AndroidJsonFactory.getDefaultInstance(), null);
final Urlshortener urlshortener = builderShortener.build();
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
Url url = new Url();
url.setLongUrl(uri.toString());
try {
Urlshortener.Url.Insert insert=urlshortener.url().insert(url);
insert.setKey("YOUR_API_KEY");
url = insert.execute();
Log.e("url.getId()", url.getId());
return url.getId();
} catch (IOException e) {
e.printStackTrace();
return uri.toString();
}
}
@Override
protected void onPostExecute(String dynamicLink) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_subject));
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, dynamicLink);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
Log.e("dynamicLink", dynamicLink);
}
}.execute(null, null, null);
}
希望对您有所帮助!!
这可以使用 Firebase 动态链接 REST 以编程方式完成 API,例如:
POST https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=api_key
Content-Type: application/json
{
"longDynamicLink": "https://abc123.app.goo.gl/?link=https://example.com/&apn=com.example.android&ibi=com.example.ios"
}
见https://firebase.google.com/docs/dynamic-links/short-links
我只需要为 Android 编写代码 - 这是代码,以防对某人有帮助:
在activity的顶部:
lateinit private var dynamicLinkApi: FbDynamicLinkApi
private var remoteCallSub: Subscription? = null // in case activity is destroyed after remote call made
在 onCreate 中(不是真的,但为了简单起见,实际上你应该注入它):
val BASE_URL = "https://firebasedynamiclinks.googleapis.com/"
val retrofit = Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
.build()
dynamicLinkApi = retrofit.create(FbDynamicLinkApi::class.java)
然后是时候缩短 URL:
remoteCallSub = dynamicLinkApi.shortenUrl(getString(R.string.fbWebApiKey), UrlRo(dynamicLink))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ url -> Log.d("Shortened: $url") },
{ e -> Log.e("APP","Error with dynamic link REST api", e) })
不要忘记在 onDestroy 中取消订阅:
override fun onDestroy() {
super.onDestroy()
remoteCallSub?.unsubscribe()
}
这是动态 API 所需的 类:
interface FbDynamicLinkApi {
@POST("v1/shortLinks")
fun shortenUrl(@Query("key") key: String, @Body urlRo: UrlRo): Observable<ShortUrlRo>
}
data class UrlRo(val longDynamicLink: String, val suffix: SuffixRo = SuffixRo())
data class SuffixRo(val option: String = "UNGUESSABLE")
data class ShortUrlRo(val shortLink: String, val warnings: List<WarningRo>, val previewLink: String)
data class WarningRo(val warningCode: String, val warningMessage: String)
试试这个,在我的情况下效果很好,
https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=[api-key]
{
"dynamicLinkInfo": {
"dynamicLinkDomain": "peg3z.app.goo.gl",
"link": "[Your Long Url Which you want to make short]",
"androidInfo": {
"androidPackageName": "com.xyz"//
},
"iosInfo": {
"iosBundleId": "com.dci.xyz"
}
}
}
'Content-Type: text/plain'
可以缩短 Firebase 深层链接的链接吗?他们有这个功能吗?生成的链接太长,这样不好。
可以在 Firebase 控制台的“动态链接”选项卡中缩短 link。点击 'New Dynamic Link',您可以选择从现有的 link.
创建短 link目前无法以编程方式完成此操作。
更新
Firebase 现在支持缩短动态 links programmatically.
在以编程方式创建动态 link 时,我遇到了同样的问题,变得冗长且用户不友好 URL。
我找到的解决方案是使用效果很好的 Google URL Shortener API。 link 指向 Java 库,我在 Android 中使用它,但你也可以做一个简单的 http 请求。
我会 post 我的 Android 代码以备不时之需:
private void createDynamicLink() {
// 1. Create the dynamic link as usual
String packageName = getApplicationContext().getPackageName();
String deepLink = "YOUR DEEPLINK";
Uri.Builder builder = new Uri.Builder()
.scheme("https")
.authority(YOUR_DL_DOMAIN)
.path("/")
.appendQueryParameter("link", deepLink)
.appendQueryParameter("apn", packageName);
final Uri uri = builder.build();
//2. Create a shorten URL from the dynamic link created.
Urlshortener.Builder builderShortener = new Urlshortener.Builder (AndroidHttp.newCompatibleTransport(), AndroidJsonFactory.getDefaultInstance(), null);
final Urlshortener urlshortener = builderShortener.build();
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
Url url = new Url();
url.setLongUrl(uri.toString());
try {
Urlshortener.Url.Insert insert=urlshortener.url().insert(url);
insert.setKey("YOUR_API_KEY");
url = insert.execute();
Log.e("url.getId()", url.getId());
return url.getId();
} catch (IOException e) {
e.printStackTrace();
return uri.toString();
}
}
@Override
protected void onPostExecute(String dynamicLink) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_subject));
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, dynamicLink);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
Log.e("dynamicLink", dynamicLink);
}
}.execute(null, null, null);
}
希望对您有所帮助!!
这可以使用 Firebase 动态链接 REST 以编程方式完成 API,例如:
POST https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=api_key
Content-Type: application/json
{
"longDynamicLink": "https://abc123.app.goo.gl/?link=https://example.com/&apn=com.example.android&ibi=com.example.ios"
}
见https://firebase.google.com/docs/dynamic-links/short-links
我只需要为 Android 编写代码 - 这是代码,以防对某人有帮助:
在activity的顶部:
lateinit private var dynamicLinkApi: FbDynamicLinkApi
private var remoteCallSub: Subscription? = null // in case activity is destroyed after remote call made
在 onCreate 中(不是真的,但为了简单起见,实际上你应该注入它):
val BASE_URL = "https://firebasedynamiclinks.googleapis.com/"
val retrofit = Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
.build()
dynamicLinkApi = retrofit.create(FbDynamicLinkApi::class.java)
然后是时候缩短 URL:
remoteCallSub = dynamicLinkApi.shortenUrl(getString(R.string.fbWebApiKey), UrlRo(dynamicLink))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ url -> Log.d("Shortened: $url") },
{ e -> Log.e("APP","Error with dynamic link REST api", e) })
不要忘记在 onDestroy 中取消订阅:
override fun onDestroy() {
super.onDestroy()
remoteCallSub?.unsubscribe()
}
这是动态 API 所需的 类:
interface FbDynamicLinkApi {
@POST("v1/shortLinks")
fun shortenUrl(@Query("key") key: String, @Body urlRo: UrlRo): Observable<ShortUrlRo>
}
data class UrlRo(val longDynamicLink: String, val suffix: SuffixRo = SuffixRo())
data class SuffixRo(val option: String = "UNGUESSABLE")
data class ShortUrlRo(val shortLink: String, val warnings: List<WarningRo>, val previewLink: String)
data class WarningRo(val warningCode: String, val warningMessage: String)
试试这个,在我的情况下效果很好, https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=[api-key]
{
"dynamicLinkInfo": {
"dynamicLinkDomain": "peg3z.app.goo.gl",
"link": "[Your Long Url Which you want to make short]",
"androidInfo": {
"androidPackageName": "com.xyz"//
},
"iosInfo": {
"iosBundleId": "com.dci.xyz"
}
}
}
'Content-Type: text/plain'