Firebase dynamic link support custom parameters?

寵の児 提交于 2019-11-30 13:11:27

问题


I am writing a App for a Open Source Conference.

Originally each attendees will receive different link via email or SMS like

https://example.com/?token=fccfc8bfa07643a1ca8015cbe74f5f17

then use this link to open app, we can know the user is which attendee by the token.

Firebase release a new feature Dynamic Links in I/O 2016, it provide better experience for users.

I had try that, but I can't find any way to pass the custom parameters (the token) in dynamic links, how to use the same link with different parameters to my users?

Thanks.


回答1:


I don't think you can use the short url: https://<my app>.app.goo.gl/Gk3m unless you create one for each user, but you can use the long url: https://<my app>.app.goo.gl/?link=https://example.com/?token=fccfc8bfa07643a1ca8015cbe74f5f17 ...(add other parameters as needed) and set new token for each user.

I assume you generate the tokens automatically. In that case you can use this to shorten the links.




回答2:


Now you can create short links using the Firebase SDK through the FirebaseDynamicLinks.getInstance().createDynamicLink(): https://firebase.google.com/docs/dynamic-links/android/create

Sample code:

Task shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
    .setLink(Uri.parse("https://example.com/"))
    .setDynamicLinkDomain("abc123.app.goo.gl")
    // Set parameters
    // ...
    .buildShortDynamicLink()
    .addOnCompleteListener(this, new OnCompleteListener() {
        @Override
        public void onComplete(@NonNull Task task) {
            if (task.isSuccessful()) {
                // Short link created
                Uri shortLink = task.getResult().getShortLink();
                Uri flowchartLink = task.getResult().getPreviewLink();
            } else {
                // Error
            }
        }
    });



回答3:


If you want to use dynamic links with custom arguments with REST, here is an example of a payload:

{
  "dynamicLinkInfo": {
        "dynamicLinkDomain": "example.app.goo.gl",
        "link": "http://someurl.com?my_first_param=test&my_second_param=test2"
  },
  "suffix": {
     "option":"UNGUESSABLE"
    }
}

Make sure your remove 'https://' from your dynamicLinkDomain

Julien




回答4:


You can add extra parameter to your link to generate Short URL from Firebase. Here I given example of Short URL generation using Firebase API. Here ServiceRequestHelper(this).PostCall is my generic class to make API request

String url = "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=YOUR_KEY";

    try {
        PackageManager manager = this.getPackageManager();
        PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
        JSONObject jsonObject = new JSONObject();
        JSONObject androidInfoObject = new JSONObject();
        androidInfoObject.put("androidPackageName", getApplicationContext().getPackageName());
        androidInfoObject.put("androidMinPackageVersionCode",String.valueOf(info.versionCode));

        JSONObject iosInfoObject = new JSONObject();
        iosInfoObject.put("iosBundleId", "tettares.Test_ID");
        JSONObject dynamicLinkInfoObj = new JSONObject();
        dynamicLinkInfoObj.put("dynamicLinkDomain", "wgv3v.app.goo.gl");
        dynamicLinkInfoObj.put("link", "https://test.in/?UserId=14&UserName=Naveen");  // Pass your extra paramters to here
        dynamicLinkInfoObj.put("androidInfo", androidInfoObject);
        dynamicLinkInfoObj.put("iosInfo", iosInfoObject);

        JSONObject suffixObject = new JSONObject();
        suffixObject.put("option" , "SHORT");
        jsonObject.put("dynamicLinkInfo", dynamicLinkInfoObj);
        jsonObject.put("suffix", suffixObject);

        Log.d("JSON Object : " , jsonObject.toString());

        new ServiceRequestHelper(this).PostCall(url, jsonObject, false, new CallBackJson() {
            @Override
            public void done(JSONObject result) throws JSONException {

                try {
                    if (result.has("shortLink")) {
                        DEEP_LINK_URL = result.getString("shortLink");                                                   }
                } catch(Exception e)
                {
                    e.printStackTrace();
                }

            }
        });


    } catch (JSONException | PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

In Your Receiving Activity:

boolean autoLaunchDeepLink = false;
    AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink)
            .setResultCallback(
                    new ResultCallback<AppInviteInvitationResult>() {
                        @Override
                        public void onResult(@NonNull AppInviteInvitationResult result) {
                            if (result.getStatus().isSuccess()) {
                                // Extract deep link from Intent
                                Intent intent = result.getInvitationIntent();
                                String deepLink = AppInviteReferral.getDeepLink(intent);

                                // 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
                                Log.d(TAG, "deeplink URL: " + deeplink); // Here you get https://test.in/?UserId=14&UserName=Naveen
                                // [END_EXCLUDE]
                            } else {
                                Log.d(TAG, "getInvitation: no deep link found.");
                            }
                        }
                    });



回答5:


I tried all above but none work. So I think you should change the link http://example.com/?userid=123tohttp://example.com/userid/123




回答6:


1 First Change your Dynamic Link in firebase console from http://exampleandroid/test to http://exampleandroid/test?data 2. You send the query paramter data with this

 DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
                   // .setLink(dynamicLinkUri)
                    .setLink(Uri.parse("http://exampleandroid/test?data=dsads"))
                    .setDomainUriPrefix("https://App_Name.page.link")
                    // Open links with this app on Android
                    .setAndroidParameters(new DynamicLink.AndroidParameters.Builder().build())
                    // Open links with com.example.ios on iOS
                    .setIosParameters(new DynamicLink.IosParameters.Builder("com.appinventiv.ios").build())
                    .buildDynamicLink();

            dynamicLinkUri = dynamicLink.getUri();



回答7:


No need of all the hustle

  1. Don't shorten the url if you want to pass parameters
  2. Write the links like this.

    //APP_CODE is firebase link
    String link = "https://APP_CODE.app.goo.gl/?refferer=" + userId;
    
    Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
                    .setMessage(getString(R.string.invitation_custom_message)))
                    .setDeepLink(Uri.parse(link))
                    .setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
                    .setCallToActionText(getString(R.string.invitation_cta))
                    .build();
    
    startActivityForResult(intent, REQUEST_INVITE);
    



回答8:


For web apps, which generating dynamic links.

const data = {
  dynamicLinkInfo: {
    domainUriPrefix: 'subdomain.page.link',
    link: url,
  },
  suffix: {
    option: 'SHORT'
  }
};
return fetch(`https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=${environment.firebaseConfig.apiKey}`, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(data),
}).then(res => res.json()).then(response => response.shortLink).catch(console.error);


来源:https://stackoverflow.com/questions/38284518/firebase-dynamic-link-support-custom-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!