How get extra parameter from dynamic link using Firebase? - android

I have created dynamic link manually and i set some additional parameters on the link, like this: https://XXXXX.goo.gl/?link=https%3A%2F%2Fairbanq.send.com%2Fsendmoney&apn=com.xxxx.xxxx&amv=1&username=Adri&amount=7.00
But when the app is opened i just get: "https:// airbanq.send.com/sendmoney"
without the addiotional parameters
i am using this sample code
https://github.com/firebase/quickstart-android/tree/master/dynamiclinks
any help please,
Thanks
My code
public String buildDeepLink() {
// Get the unique appcode for this app.
String appCode = AirBanqApp.mContext.getString(R.string.app_code);
// Get this app's package name.
String packageName = AirBanqApp.mContext.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)
.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));
}
if (!TextUtils.isEmpty(androidLink)) {
builder.appendQueryParameter("al", androidLink);
}
if (!TextUtils.isEmpty(playStoreAppLink)) {
builder.appendQueryParameter("afl", playStoreAppLink);
}
if (!customParameters.isEmpty()) {
for (Map.Entry<String, String> parameter : customParameters.entrySet()) {
builder.appendQueryParameter(parameter.getKey(), parameter.getValue());
}
}
// Return the completed deep link.
return builder.build().toString();
}

Thats was my solution
i solved my issue, i assumed the "apn", "username" and "amount" they were part of the parameter "LINK" in the url, but no when i add the "&" i am adding parts to the main url, to add parameters to the "LINK" field i need to create first the url like this
https://airbanq.send.com/sendmoney?username=Adri&amount=7.00
then use URLEncoder.encode(queryParameters.toString(), "UTF-8");
to generate this
https%3A%2F%2Fairbanq.send.com%2Fsendmoney%253Fusername%253DAdri%2526amount%253D7.00
and then append to main url
https://xxxx.app.goo.gl/?link=https%3A%2F%2Fairbanq.send.com%2Fsendmoney%253Fusername%253DAdri%2526amount%253D7.00&apn=com.airbanq.airbanqapp&amv=1
public String buildDeepLink() {
// Get the unique appcode for this app.
String appCode = AirBanqApp.mContext.getString(R.string.app_code);
// Get this app's package name.
String packageName = AirBanqApp.mContext.getPackageName();
String queryParamters = "";
try {
queryParamters = generateQueryParameters();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (!TextUtils.isEmpty(queryParamters)) {
deepLink = deepLink + queryParamters;
}
// Build the link with all required parameters
Uri.Builder builder = new Uri.Builder()
.scheme("https")
.authority(appCode + ".app.goo.gl")
.path("/")
.appendQueryParameter("link", deepLink)
.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));
}
if (!TextUtils.isEmpty(androidLink)) {
builder.appendQueryParameter("al", androidLink);
}
if (!TextUtils.isEmpty(playStoreAppLink)) {
builder.appendQueryParameter("afl", playStoreAppLink);
}
// Return the completed deep link.
return builder.build().toString();
}
private String generateQueryParameters() throws UnsupportedEncodingException {
StringBuilder queryParameters = new StringBuilder();
//server purposes
queryParameters.append("?*code*");
if (!customParameters.isEmpty()) {
for (Map.Entry<String, String> parameter : customParameters.entrySet()) {
queryParameters.append(String.format("&%1s=%2s", parameter.getKey(), parameter.getValue()));
}
}
return URLEncoder.encode(queryParameters.toString(), "UTF-8");
}

The official answer is that you need to escape/encode a URL string so that it can be safely placed inside a URL query.
I wish Firebase dynamic links would just say that about the link.
For Golang:
url.QueryEscape(urlstring)

Related

Add query parameters to link in firebase dynamic link

I create dynamic link and I want to send some specific parameter, like:
"https://mydynamiclink/?link=" + link + "&msgid=" + id + "&apn=myapn".
link field looks like "https://play.google.com/store/apps/details/?id=com.myApp&msgid=myId&apn=myapn"
When I open my app after taping on this link - I receive PendingDynamicLinkData and can get link from it, but not some custom data. (pendingDynamicLinkData.getLink() returns my link without "&msgid=..." - I'm getting string "https://play.google.com/store/apps/details/?id=com.myApp")
How can I add my msgid field and get it after all?
I've found solution
String query = "";
try {
query = URLEncoder.encode(String.format("&%1s=%2s", "msgid", id), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
final String link = "https://play.google.com/store/apps/details/?id=com.myApp" + query;
After such encoding pendingDynamicLinkData.getLink() returns me https://play.google.com/store/apps/details/?id=com.myApp&msgid=myId
Accepted answer didn't work out fine for me, all i needed to do was check if the link was for a user's profile and not a blog post, so i can redirect to my ProfileActivity instead.
private void generateDynamicLink() {
//build link normally and add queries like a normal href link would
String permLink = getLink() + "?route=profile&name=" + getProfileName()
+ "&category=" + getUserPracticeCategory()
+ "&picture=" + getProfilePicture();
FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse(permLink))
.setDynamicLinkDomain(Constants.DYNAMIC_LINK_DOMAIN)
.setAndroidParameters(new
DynamicLink.AndroidParameters.Builder().build())
.setSocialMetaTagParameters(
new DynamicLink.SocialMetaTagParameters.Builder()
.setTitle("Enter Title")
.setDescription("Enter Desc here")
.setImageUrl(Uri.parse(getProfilePicture()))
.build())
.buildShortDynamicLink()
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT,task.getResult().getShortLink());
intent.setType("text/plain");
startActivity(intent);
} else {
Utils.snackBar(tvAddress, "Failed to Generate Profile Link, Try
Again");
}
});
}
and when a user navigates into my app using the generated link, it goes to a post detail activity, because i made that activity the only browsable activity in my manifest. i then have to use the route query to determine if the incoming link is a blog post or a shared user profile.
private void retrieveDynamicLink() {
FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent())
.addOnSuccessListener(this, pendingDynamicLinkData -> {
if (pendingDynamicLinkData == null) {
retrieveLocalIntent();
} else {
Toast.makeText(context, "Resolving Link, Please Wait...", Toast.LENGTH_LONG).show();
if (pendingDynamicLinkData.getLink().getQueryParameter("route") != null) {
if (Objects.requireNonNull(pendingDynamicLinkData.getLink().getQueryParameter("route")).equalsIgnoreCase("profile")) {
try {
Uri uri = pendingDynamicLinkData.getLink();
String permLink = uri.toString().split("\\?")[0];
Intent intent = new Intent(this, ProfileActivity.class);
intent.putExtra(ProfileActivity.PROFILE_NAME, uri.getQueryParameter("name"));
intent.putExtra(ProfileActivity.PROFILE_CATEGORY, uri.getQueryParameter("category"));
intent.putExtra(ProfileActivity.PROFILE_PICTURE, uri.getQueryParameter("picture"));
intent.putExtra(Utils.POST_PERMLINK, permLink);
startActivity(intent);
this.finish();
} catch (NullPointerException e) {
Toast.makeText(context, "Unable to View User Profile", Toast.LENGTH_SHORT).show();
}
}
} else {
postHrefLink = pendingDynamicLinkData.getLink().toString();
getPostDetail.getData(postHrefLink);
}
}
})
.addOnFailureListener(this, e ->
retrieveLocalIntent()
);
}
Hope this helps.
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();
Let's say that You want to create the following URL:
https://www.myawesomesite.com/turtles/types?type=1&sort=relevance#section-name
For this you can do following
Uri.Builder builder = new Uri.Builder();
builder.scheme("https")
.authority("www.myawesomesite.com")
.appendPath("turtles")
.appendPath("types")
.appendQueryParameter("type", "1")
.appendQueryParameter("sort", "relevance")
.fragment("section-name");
String myUrl = builder.build().toString();

How to check if the string is valid Url path format

Understands that using Patterns.WEB_URL.matcher(url).matches() will able to validate if that string is a valid url, however it will required in a full format which contain https: / .com.
In my case, i want to validate if the json return a string in correct format for path e.g /images/slider/my/myImage.jpg; which does not contain https or any. How can i do this?
What i want to do is something like:
if(ImageUrl equal "/images/slider/my/myImage.jpg" FORMAT) {
//Do something here
} else //ImageUrl = myImage.jpg {
//Add "/images/slider/my/" infront of the text
}
P.s: My image link will be like www.abc.com/images/slider/my/myImage.jpg
Use URLUtil to validate the URL as below.
URLUtil.isValidUrl(url)
It will return True if URL is valid and false if URL is invalid.
Another way is given below.
public static boolean checkURL(CharSequence input) {
if (TextUtils.isEmpty(input)) {
return false;
}
Pattern URL_PATTERN = Patterns.WEB_URL;
boolean isURL = URL_PATTERN.matcher(input).matches();
if (!isURL) {
String urlString = input + "";
if (URLUtil.isNetworkUrl(urlString)) {
try {
new URL(urlString);
isURL = true;
} catch (Exception e) {
}
}
}
return isURL;
}
This link will explain how you can check the url is available or not.
For more about URLS please visit this
Please have a try

Accessing all response headers using volley api in android application

while making connection using HttpClient in android from HttpResponse able to get all possible "set-cookie" header value (JESSIONID and XSCRF-TOKEN).Check below screenshot.
Now working with android studio with volley api for connection , i am getting only single value of "set-cookie" header (JESSIONID only).See below :
I have also check https://groups.google.com/forum/#!topic/volley-users/rNTlV-LORzY.
For which have to make change in volley api jar project. But don't know how to edit volley api. If any other solution present kindly guide.
Kindly help to retrieve multiple value of "set-cookie" using volley api.
Problem:
The problem is inside the Volley unfortunately. I had this problem and after many searches i figured out that there is a method called convertHeaders in BasicNetwork class that handles headers like this:
protected static Map<String, String> convertHeaders(Header[] headers) {
Map<String, String> result = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
for (int i = 0; i < headers.length; i++) {
result.put(headers[i].getName(), headers[i].getValue());
}
return result;
}
You see the result is Map<String, String> which can't contain same keys with different values. so you always have only last cookie.
The standard of cookie setting tells us we should separate cookies with ; for example if you want to contain 2 key-value in a request cookie you should put them like this:
Cookies: k1=v1;k2=v2
Solution:
In your case you have two options.
1 - change your code in Server-Side so that the response contains only 1 Set-Cookie separated key-values by ;. example of your response:
Set-Cookie: JESSIONID=qZtQ...;Path=/;HttpOnly;XSRF-TOKEN=6c65...
2 - get Volley source code and change that buggy method and make a fixed .jar again! this option is my favorite cause you didn't touch the response of server
My implementation of this method is:
protected static Map<String, String> convertHeaders(Header[] headers) {
TreeMap result = new TreeMap(String.CASE_INSENSITIVE_ORDER);
for(int i = 0; i < headers.length; ++i) {
String headerName = headers[i].getName();
if(!result.containsKey(headerName)) {
result.put(headers[i].getName(), headers[i].getValue());
} else {
String value = (String)result.get(headerName);
String mergedValue = value + ";" + headers[i].getValue();
result.remove(headerName);
result.put(headerName, mergedValue);
}
}
return result;
}
There is workaround for this in:
implementation "com.android.volley:volley:1.1.0"
"NetworkResponse (and Cache.Entry) now includes an "allHeaders" field which is the raw list of all headers returned by the server and thus can include duplicates by name."
Source: https://github.com/google/volley/issues/21
Example:
private static final String COOKIE_KEY = "Set-Cookie";
private static final String COOKIE_NAME = "NameOfOneOfTheCookies";
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
handleCookies(response);
String parsed;
try {
parsed = new String(response.data, "utf-8");
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
return Response.success(parsed,
HttpHeaderParser.parseCacheHeaders(response));
}
private void handleCookies(NetworkResponse response) {
for (Header header : response.allHeaders) {
if (header.getName().equals(COOKIE_KEY) && header.getValue().startsWith(COOKIE_NAME)) {
getCookies(response);
}
}
}
private void getCookies(NetworkResponse response) {
ArrayList<String> cookiesList = new ArrayList<>();
for (Header header : response.allHeaders) {
if (header.getName().equals(COOKIE_KEY)) {
cookiesList.add(header.getValue());
}
}
// TODO Do something with the cookiesList
}

Custom API in Azure APP Serivce examples searched for Android Client

I need a working example for a custom API for Microsoft Azure App Service.
I could not get any useful or working information/examples for that, or they just show each time different approaches which are outdated?!?!
For now I have a working table controller which gets information from database and returns it back to my Android client. Now I need to define a custom API Controller to get a string back. In the examples they are all sending an object to the service in order to get an object back. I do not want to send anything to the API, just retrieve some information back from a GET Request.
Regards
// EDIT - Added / edited client / server code to Post a String.
You can use the following code to do a GET request on the auto generated API controller Visual Studio creates (ValuesController).
private void getStringFromAzure() throws MalformedURLException {
// Create the MobileService Client object and set your backend URL
String yourURL = "https://yourApp.azurewebsites.net/";
MobileServiceClient mClient = new MobileServiceClient(yourURL, this);
// Your query pointing to yourURL/api/values
ListenableFuture<JsonElement> query = mClient.invokeApi("values", null, GetMethod, null);
// Callback method
Futures.addCallback(query, new FutureCallback<JsonElement>() {
#Override
public void onSuccess(JsonElement jsonElement) {
// You are expecting a String you can just output the result.
final String result = jsonElement.toString();
// Since you are on a async task, you need to show the result on the UI thread
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onFailure(Throwable throwable) {
Log.d(TAG, "onFailure: " + throwable.getMessage());
}
});
}
public void sendString(final String someString) throws MalformedURLException {
// Your query pointing to /api/values/{String}
ListenableFuture<JsonElement> query = mClient.invokeApi("values/" + someString, null, PostMethod, null);
// Callback method
Futures.addCallback(query, new FutureCallback<JsonElement>() {
#Override
public void onSuccess(JsonElement jsonElement) {
// You are expecting a String you can just output the result.
final String result = jsonElement.toString();
}
#Override
public void onFailure(Throwable throwable) { }
});
}
The backend API: (ValuesController)
{
// Use the MobileAppController attribute for each ApiController you want to use
// from your mobile clients
[MobileAppController]
public class ValuesController : ApiController
{
// GET api/values
public string Get()
{
return "Hello World!";
}
// POST api/values/inputString
public string Post(string inputString)
{
return inputString;
}
}
}
You can also send parameters along in the following way:
List<Pair<String, String>> parameters = new ArrayList<>();
parameters.add(new Pair<>("name", "John"));
parameters.add(new Pair<>("password", "fourwordsalluppercase"));
ListenableFuture<JsonElement> query = client.invokeApi("yourAPI", PostMethod, parameters);
Or as json in the body:
JsonObject body = new JsonObject();
body.addProperty("currentPassword", currentPassword);
body.addProperty("password", password);
body.addProperty("confirmPassword", confirmPassword);
ListenableFuture<JsonElement> query = mClient.invokeApi("yourAPI", body, PostMethod, null);
Based on my understanding, I think there are two parts in your question which include as below. And I think you can separately refer to two sections to get the answers and write your own example.
How to define a custom API on Azure Mobile App to retrieve data from database? Please refer to the section Custom APIs to know how to do with Azure Mobile App backend.
How to call a custom API from Android App? Please refer to the section How to: Call a custom API to know how to do with Android SDK.

Unclear instruction in Google Documents List API "Creating or uploading spreadsheets"

I'm trying to create a spreadsheet via my android app. So far I've been only successful in creating an empty text document. Google's Spreadsheet API instructs me to follow Google Documents List API in order to create a spreadsheet document in Google Drive. In Google Documents List API it says:
To create a new, empty spreadsheet, follow the instructions in
Creating a new document or file with metadata only. When doing so, use
a category term of http://schemas.google.com/docs/2007#spreadsheet.
In the link above I've found the next .NET code:
using System;
using Google.GData.Client;
using Google.GData.Documents;
namespace MyDocumentsListIntegration
{
class Program
{
static void Main(string[] args)
{
DocumentsService service = new DocumentsService("MyDocumentsListIntegration-v1");
// TODO: Authorize the service object for a specific user (see Authorizing requests)
// Instantiate a DocumentEntry object to be inserted.
DocumentEntry entry = new DocumentEntry();
// Set the document title
entry.Title.Text = "Legal Contract";
// Add the document category
entry.Categories.Add(DocumentEntry.DOCUMENT_CATEGORY);
// Make a request to the API and create the document.
DocumentEntry newEntry = service.Insert(
DocumentsListQuery.documentsBaseUri, entry);
}
}
}
I've used this code to try and create a spreadsheet, but only the third variant worked (using DocsService, without adding Category and using feedUrl URL object).
Here's part of my working code (upload() is being called when user clicks a button):
private void upload() {
SpreadSheetAsyncTask sprdSheetAsyncTsk = new SpreadSheetAsyncTask();
sprdSheetAsyncTsk.execute();
}
public class SpreadSheetAsyncTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
spreadSheetService = new SpreadsheetService("Salary_Calculator");
docService = new DocsService("Salary_Calculator");
try {
spreadSheetService.setUserCredentials(USERNAME, PASSWORD);
docService.setUserCredentials(USERNAME, PASSWORD);
URL feedUrl = new URL(
"https://docs.google.com/feeds/default/private/full/");
URL tmpFeedUrl = new URL(
"https://spreadsheets.google.com/feeds/worksheets/key/private/full");
DocumentEntry entry = new DocumentEntry();
Calendar timeRightNow = GregorianCalendar.getInstance();
entry.setTitle(new PlainTextConstruct(
"Salary Calculator Spreadsheet "
+ timeRightNow.get(Calendar.DAY_OF_MONTH) + "/"
+ (timeRightNow.get(Calendar.MONTH) + 1) + "/"
+ timeRightNow.get(Calendar.YEAR)));
// Category object = new Category("spreadsheet",
// "http://schemas.google.com/docs/2007#spreadsheet");
//
//
// entry.getCategories().add(object);
/*
* TODO TEST AREA
*/
entry = docService.insert(feedUrl, entry);
/*
* TODO TEST AREA
*/
} catch (AuthenticationException e) {
cancel(true);
loginDialog("Wrong username or password");
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (ServiceException e) {
cancel(true);
loginDialog("Wrong username or password");
} catch (Exception e) {
loginDialog("Wrong username or password");
}
return null;
}
}
The non working code uses the category object object (as shown in the code comment) above and uses entry = spreadSheetService.insert(feedUrl, entry);
My question is - what did they want me to do when they wrote use a category term of http://schemas.google.com/docs/2007#spreadsheet?

Categories

Resources