Start whatsapp from webview - android

I have an app that loads a newspaper site in a webview. In that site you can share a news in facebook, twitter and whatsapp. I have facebook's and twitter's links covered with
shouldOverrideUrlLoading()
I can't figer out how to launch the whatsapp app. It uses a custom URL scheme:
whatsapp://send?text=
I get: the webpage at whatsapp://send?text= could not be loaded because:
net::ERR_UNKNOWN_URL_SCHEME

webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("tel:") || url.contains("https://wa.me/")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
});

I suggest using loop from this question: using Intent.ACTION would give You best results.

Don't use wa.me as a domain for sharing texts on whatsapp. Just test this URL here yourself: http://wa.me/?text=mytest. I see:
ERROR
PAGE NOT FOUND
Don't use the wa.me domain. Use the api.whatsapp.com domain. wa.me requires that you use a phone number. Well, typically, you want the share URL to be shared to other people that the user knows, so, you'd want to leave that blank. Check it out...
https://api.whatsapp.com/send?text=YourShareTextHere
https://api.whatsapp.com/send?text=YourShareTextHere&phone=123
Works for me! Hope this helps someone out there!
If you are interested in watching a project that keeps track of these URLs, then check us out!: https://github.com/bradvin/social-share-urls#whatsapp

be sure to have a proper URL encoded string after text=

I've just found that it is possible to open a conversation to a number Using Click to Chat
To create your own link with a pre-filled message that will
automatically appear in the text field of a chat, use
https://wa.me/whatsappphonenumber/?text=urlencodedtext where
whatsappphonenumber is a full phone number in international format and
URL-encodedtext is the URL-encoded pre-filled message.
Example:https://wa.me/15551234567?text=I'm%20interested%20in%20your%20car%20for%20sale
NOTE: It opens WhastApp application if you click the link using a mobile phone browser (at least from Android)

this.webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("https://wa.me")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url.replace("+",""))));
return true;
} else {
return false;
}
}
});

Related

How to open the Amazon AppStore directly from my Android application?

I have seen answers regarding Amazon Appstore from iPhone but not from Android. I'm trying to make a button that will open the Amazon Appstore (for android) on my app's page.
Here is how it's done for Google Play:
final String appPackageName = context.getPackageName();
try {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
} catch (android.content.ActivityNotFoundException e) {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
}
How should I replace the Strings so that it works "the same" for the Amazon AppStore?
The Amazon app store URI is
amzn://apps/android?
Directing to a particular app would look like
amzn://apps/android?p=com.amazon.mp3
Source: Linking To the Amazon Appstore for Android
Look into WebViews. It does exactly what you want, you can open a page in your own app. Simply define a webview in the xml, and use the java to display the page.
How to do it without webview:
(From docs)
By default, a WebView provides no browser-like widgets, does not
enable JavaScript and web page errors are ignored. If your goal is
only to display some HTML as a part of your UI, this is probably fine;
the user won't need to interact with the web page beyond reading it,
and the web page won't need to interact with the user. If you actually
want a full-blown web browser, then you probably want to invoke the
Browser application with a URL Intent rather than show it with a
WebView. For example:
Uri uri = Uri.parse("http://www.example.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
With webview:
webview.loadUrl("http://slashdot.org/");
// OR, you can also load from an HTML string:
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
Let me know if this works by commenting.

Android: 3d-secure redirect response

I have an Android app where I am processing payments within the app. The payment also requires 3d-secure verification sometimes. So this requires redirecting the user to a webpage where they will be able to make some appropriate actions: Such as entering a code or such. In my case, the app is targeted towards Swedish users and it redirects them to a page where they must open another "bank ID" app, either on the same device or another, to perform this verification.
On our iOS app this feature works as expected. Once the user has performed the verification, the browser receives a callback which can then be used to update the app accordingly, but on Android, the WebView I am using is not notified. So I am unable, so far, to handle the user-verification event.
Does anybody have experience with this or any similar use-case? Any help is appreciated.
We have experienced a similar issue with Nordea's 3D Secure page in an Android WebView.
It came down to the page trying to access local storage. We added the code below to the app to get it to work:
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setDatabaseEnabled(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
mWebView.getSettings().setDatabasePath("/data/data/" +
mWebView.getContext().getPackageName() + "/databases/");
}
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("intent:")){
Intent intent = new Intent();
intent.setPackage("com.bankid.bus");
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("bankid");
intent.setData(Uri.parse("bankid://www.bankid.com?redirect=null")) ;
startActivityForResult(intent, 0);
return true;
}
// your existing override code goes here probably "return false"
// to stop webview redirects to browser.
}
});
mWebView.loadUrl(url);

How can a website owner know that I'm visiting his page using an android app?

I want to create an android app. When I click a row in the ListView, it should go to a website.
In the ListView I'll have my widgets such as TextView, ImageView etc...
For eg: "www.amazon.com"
I tried the following,
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.EMPTY.parse("www.amazon.com"));
context.startActivity(i);
}
});
It successfully redirects to "www.amazon.com".
Here, how will the amazon people know that people are visiting their website using my app?
When you click a link, your browser loads the web page you clicked and tells the website where you came from. For example, if you clicked a link to an outside website on stackoverflow.com, the outside website would see the address of the stackoverflow.com question you came from. This information is contained in the HTTP referrer header.
To add this information you can use the following code, assuming you are using a WebView to display the website
String url = "http://www.amazon.com/";
Map<String, String> extraHeaders = new HashMap<String, String>();
extraHeaders.put("Referer", "http://www.yourwebsite.com");
WebView wv;
wv = (WebView) findViewById(R.id.webview);
wv.loadUrl(url, extraHeaders);
Using an Intent is not possible with the exception the user would use the default browser which supports this kind of code
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
Bundle bundle = new Bundle();
bundle.putString("Referer", "http://www.yourwebsite.com");
browserIntent.putExtra(Browser.EXTRA_HEADERS, bundle);
However if you simply have the WebView in your layout, with visibility invisible, and you do not overwrite the WebClient, i.e.wv.setWebViewClient(new MyWebViewClient());, you get the same behaviour as with the Intent
I've never worked with android apps so I don't know the exact implementation.
But what you could do is setting the header of your webrequest manually so you look like a desktopbrowser. If you're using chrome as a browser you can see what your headers are in the developers tools window under network

How to register new application for Instagram?

I'm trying to create an Android app that uses Instagram API. I am required to register a new application on Instagram website to obtain the client id, client secret etc. but it asks me for something called OAuth redirect_uri. I'm not familiar with what that is and how to obtain one. I don't have a website and this OAuth redirect_uri is required at registration. Could you help me out?
The redirect uri is the uri (usually a url) to where Instagram will redirect you to after you enter your login details.
So in your WebView once you enter login credentials, it goes to the redirect_uri with added parameters based on whether the login was successful or not.
Since you're on Android though, you need to get these parameters from your webview somehow. What you can do is something like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(REDIRECT_URI)) {
// Get parameters
Uri uri = Uri.parse(url);
String problem = uri.getQueryParameter("oauth_problem");
String verifier = uri.getQueryParameter("oauth_verifier");
// Do stuff, then maybe call some method to close the webview
} else
view.loadUrl(url);
return true;
}
});
What should your redirect uri be? I'm not sure, but all valid URIs I've tried seem to work.
You should probably make it unique to your app, like com-example-myapp://instagramredirect

Launch custom Android application from WebView

I have an HTML file which is launching an app if I open it in the Android native browser,
but when I try to open the same in a WebView, it is not able to launch that application, and "Webpage not available" is shown. I think my WebView is not able to handle the scheme "my.special.scheme://" defined for the application.
I read Launching an Android Application from the Browser, but it does not cover information about launching an app from a WebView.
It's true, links with a custom URI scheme don't load automatically launch apps from a WebView.
What you need to do is add a custom WebViewClient to your WebView:
webView.setWebViewClient(new CustomWebViewClient());
and then in the shouldOverrideUrlLoading(), have the following code:
public boolean shouldOverrideUrlLoading(final WebView webView, final String url) {
if (url.startsWith("my.special.scheme://")) {
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
// The following flags launch the app outside the current app
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
return true;
}
return false;
}
I'm not sure, but I believe that WebView simply doesn't handle custom URI schemes.
The workaround is to override WebViewClient.shouldOverrideUrlLoading() and manually test if the URL uses your URI scheme, launching your app and returning true if it matches, otherwise returning false.
I perform an ACTION_VIEW with the URL to make the URL open in the default browser, which will do the redirecting to the concerning app (I had to fix this concerning payments via a bank app)

Categories

Resources