Android Code Snippet Get Firebase shortlink from long dynamic link

Code to get a shortlink from a long dynamic deeplink via "Firebase Dynamic Links REST API"

Dynamic links are usually very long, unsightly and reveal unnecessarily it's technical details:
24-09-_2017_19-44-01.png


Shortlinks look alot better (e.g. in an invite message):
24-09-_2017_17-54-48.png

While there are several variants of shortlink functions out there, this solution is very well suited for users of the cuddly Firebase world.

B4X:
Sub GetFibaShortDynamicLink(strLongDynamicLink As String) As ResumableSub
    ' --> https://firebase.google.com/docs/dynamic-links/rest#create_a_short_link_from_a_long_link
    Dim mapData As Map
    mapData.Initialize
    mapData.Put("longDynamicLink", strLongDynamicLink)
    Dim jsonGen As JSONGenerator
    jsonGen.Initialize(mapData)
    Dim strJsonToSend As String = jsonGen.ToString
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString("https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=" & Starter.strFirebaseWebApiKey, strJsonToSend) ' <-- https://console.firebase.google.com/project/yyyyyyyourproject-yyyyyyyy/settings/general/android:yyyyyy.yourpackage.yyyyy
    j.GetRequest.SetContentType("application/json")
    wait for (j) JobDone(job As HttpJob)
    Dim shortUrl As String = ""
    If job.Success Then
        Dim jp As JSONParser
        jp.Initialize(job.GetString)
        shortUrl = jp.NextObject.Get("shortLink")
    Else
        Log("GFDSL-01")
    End If
    job.Release
    Return shortUrl
End Sub

B4X:
Dim sb As StringBuilder
sb.Initialize
sb.Append("https://").Append(Starter.app_code).Append(".app.goo.gl/")
sb.Append("?link=").Append(Starter.strBN_SL01 & "?data=" & strDownloadUrlHttps)
sb.Append("&apn=").Append(Starter.strAppPackNam)
sb.Append("&amv=").Append("2034")
'
Dim strDynamicLinkMan As String = sb.ToString
wait for(  GetFibaShortDynamicLink(strDynamicLinkMan)  ) complete (strShortLink As String)
Log("#-  x49257, strShortLink=" & strShortLink)

You will need a "Web API key" to authenticate your requests to the API and store it in
"strFirebaseWebApiKey":


24-09-_2017_19-36-43.png
 
Last edited:
Top