3

I'm creating a Firebase dynamic/deep link manually like this:

Uri BASE_URI = Uri.parse("http://example.com/");
String packageName = getBaseContext().getPackageName();
Uri APP_URI = BASE_URI.buildUpon().path(requestID.getText().toString().trim()+"%3DrequestID="+requestID.getText().toString().trim()+"%3Dextra1="+extra1.getText().toString().trim()+"%3Dextra2="+extra2.getText().toString().trim()).build();
String encodedUri = URLEncoder.encode(APP_URI.toString(), "UTF-8");
Uri deepLink = Uri.parse("https://appcode.app.goo.gl/?link="+encodedUri+"&apn="+packageName+"&amv="+16+"&ad="+0);

and then I'm sharing it with another user and he is receiving this link: http://example.com/-KcP1YHgGsg3tVYybX8b%253DrequestID%3D-KcP1YHgGsg3tVYybX8b%253Dextra1%3Dtext1%253Dextra2%3D3%253Dtext2.

Then using following code I'm trying to get the query parameters (extra parameters in the link) from here:

    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();
                                        final String deepLink = AppInviteReferral.getDeepLink(intent);
                                        Log.d("deepLinkMainActivity", deepLink);

                                        Uri uri = Uri.parse(deepLink);
                                        String requestId = uri.getQueryParameter("requestID");
                                        Log.d("requestId123", requestId);
                                } else {
                                    Log.d("getInvitation", "getInvitation: no deep link found.");
                                }
                            }
                        });

but the app is getting crashed showing: java.lang.NullPointerException: println needs a message because uri.getQueryParameter("requestID"); is returning null.

What am I doing wrong and How can I get the query parameters from the deep-link?

Please let me know.

Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133

2 Answers2

7


This i your url
Uri APP_URI = BASE_URI.buildUpon().path(requestID.getText().toString().trim()+"%3DrequestID="+requestID.getText().toString().trim()+"%3Dextra1="+extra1.getText().toString().trim()+"%3Dextra2="+extra2.getText().toString().trim()).build();

If you decode this Url you are going to receive something like this

http://www.airbanq.com?name=45=extra1=45=extra2=12=extra3=455

This value %3D is equal to a =. To concatenate values in a query string you need to use this &, maybe you can try to avoid the encoding and decoding and try again. I suggested that option, in the other thread, because that was working fine to me with an older version of FIrebase.

That's the main reason behind of your null error, basically you are trying to get a query string from this http://www.airbanq.com?name=45=extra1=45=extra2=12=extra3=455

Create a link like this http://www.airbanq.com?name=45&extra1=45&extra2=12&extra3=455 and let me know. After this change with your code this will be works fine.

Update 1
I will share with you some code, hope this can solve your problem.

This code is based on what you have right now
This first part is to create the URL and add some parameters as you can see

Uri BASE_URI = Uri.parse("http://example.com/");

Uri APP_URI = BASE_URI.buildUpon().appendQueryParameter("requestID", "200").
                    appendQueryParameter("extra1", "value").
                    appendQueryParameter("extra2", "value").build();


String encodedUri = null;
try {
   encodedUri = URLEncoder.encode(APP_URI.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
Uri deepLink = Uri.parse("https://app.app.goo.gl/?link="+encodedUri+"&apn=com.xx.xx.dev");

This last part is just to receive the deeplink and read the values

AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink)
                .setResultCallback(
                        new ResultCallback<AppInviteInvitationResult>() {
                            @Override
                            public void onResult(AppInviteInvitationResult result) {
                                Log.d(TAG, "getInvitation:onResult:" + result.getStatus());
                                if (result.getStatus().isSuccess()) {
                                    // Extract information from the intent
                                    Intent intent = result.getInvitationIntent();
                                    String deepLink = AppInviteReferral.getDeepLink(intent);
                                    String invitationId = AppInviteReferral.getInvitationId(intent);

                                    try {
                                        deepLink = URLDecoder.decode(deepLink, "UTF-8");
                                    } catch (UnsupportedEncodingException e) {
                                        e.printStackTrace();
                                    }
                                    Uri uri = Uri.parse(deepLink);
                                    String requestId = uri.getQueryParameter("requestID");
                                    String requestId2 = uri.getQueryParameter("extra2");

                                    // Because autoLaunchDeepLink = true we don't have to do anything
                                    // here, but we could set that to false and manually choose
                                    // an Activity to launch to handle the deep link here.
                                    // ...
                                }
                            }
                        });

And finally this is the manifest

<activity
            android:name="com.google.firebase.quickstart.invites.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data android:host="example.com" android:scheme="http"/>
                <data android:host="example.com" android:scheme="https"/>

            </intent-filter>
        </activity>

Hope this can solve your problems

  • but here: http://stackoverflow.com/a/42111331/6144372 diidu said that `&` should be replaced with `%3D`. – Hammad Nasir Feb 09 '17 at 01:43
  • and I'm getting this after decoding the url I'm receiving: `http://example.com/-KcP1YHgGsg3tVYybX8b%3DrequestID=-KcP1YHgGsg3tVYybX8b%3Dextra1=text1%3Dextra2=3%3Dtext2` – Hammad Nasir Feb 09 '17 at 01:49
  • Did you try my suggestion? Try to use this symbol & instead of %3D and avoid de encoding/decoding for the deeplink – Bryan Acuña Núñez Feb 09 '17 at 04:03
  • Sorry, Firebase console replaces '&' with %26 and '=' with %3D. I fixed my reply in another thread. If you had used firebase console for creating the url as I recommended, you would have noticed that. – diidu Feb 09 '17 at 09:39
  • @Hammad, can you provide the new url? Will be easier for us! – Bryan Acuña Núñez Feb 09 '17 at 13:43
  • how does this: `uri.getQueryParameter()` method works? – Hammad Nasir Feb 09 '17 at 14:01
  • Is basically a search of the key that you provide, i didn't see the code. If you can provide the new url that you are getting it will be great – Bryan Acuña Núñez Feb 09 '17 at 14:56
  • I've updated the question with the new url.. also please see that I've used `uri.getQueryParameter()` and still I'm getting `null`. Why? – Hammad Nasir Feb 09 '17 at 19:07
  • Your current link is completely broken. First of all, it seems you changed & to %25 in the embedded link, while you should have changed it to %26. Also you left some or the 3D there without %. getQueryParameter does not find the parameter because your url does not have any that is properly set. Could you please use firebase console for creating the url so you get it correctly. And when you use it, give & and = normally. See this to get some idea on why the url looks like it does: http://www.w3schools.com/tags/ref_urlencode.asp – diidu Feb 10 '17 at 08:09
  • I guess I know now what you did. Perhaps you created the url with firebase now. In that case you need to give proper url, not %xx encoding. – diidu Feb 10 '17 at 08:15
  • @diidu thanks for replying again... it'd be great if you can give me an example of a link which can use `getQueryParamter()` appropriately for getting the query parameters.. Please. – Hammad Nasir Feb 11 '17 at 09:27
  • I have not used getQueryParameter so much I would know it by heart. I think it is as easy for you to read the manual page and find examples for the command as it is for me. So I leave it for you. – diidu Feb 13 '17 at 06:53
  • I am struggling to create a firebase dynamic link, could anyone read my question. https://stackoverflow.com/questions/50570676 – bibscy May 28 '18 at 18:34
0

If you want to extract a text from the link text, use regex or get at specific indexof text. This is how you can get the data from invitation: Reference

Intent intent = result.getInvitationIntent();
String deepLink = AppInviteReferral.getDeepLink(intent);
String invitationId = AppInviteReferral.getInvitationId(intent);

and it s better to paste whole error message.

Tony Babarino
  • 3,355
  • 4
  • 32
  • 44
Elias Fazel
  • 2,093
  • 16
  • 20