UPI Intent in WebView - Android
Enable UPI Intent in WebView on your Android app.
Follow the steps given below to enable UPI intent in WebView on your android application:
1.1
1.2
1.3
1.4
Add a WebView to the layout of your Android app. You can add the code given below to the layout XML file of your app:
<WebViewandroid:id="@+id/webview"android:layout_width="match_parent"android:layout_height="match_parent" />
To setup a WebViewClient
that handles URL requests, create a new class using the code given below extending the MyWebViewClient
and overriding the shouldOverrideUrlLoading()
method.
class MyWebViewClient extends WebViewClient {private Activity activity;public MyWebViewClient(Activity activity) {this.activity = activity;}/** This function is used to handle deeplink in webview */@Overridepublic Boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {return true;}}// Use CustomWebviewClient created above in the webview object.class MainActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);WebView webView = findViewById(R.id.webview);webView.setWebViewClient(new MyWebViewClient(MainActivity.this));}}
To handle deep links in the shouldOverrideUrlLoading()
method, check if the URL is a deep link and then launch the corresponding activity using the intent class. In this example, we are checking if the URL starts with http://
or https://
. If not, then we create a new intent with ACTION_VIEW
and URL as the data and start the activity with this intent.
import android.app.Activity;import android.content.ActivityNotFoundException;import android.content.Intent;import android.graphics.Bitmap;import android.net.Uri;import android.webkit.WebResourceRequest;import android.webkit.WebView;import android.webkit.WebViewClient;class MyWebViewClient extends WebViewClient {@Overridepublic void onPageFinished(WebView view, String url) {super.onPageFinished(view, url);}/** This function is used to handle deeplink in webview */@Overridepublic boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {String url = request.getUrl().toString();if (!url.startsWith("https") || !url.startsWith("http")) {try {Intent i = new Intent(Intent.ACTION_VIEW);i.setData(Uri.parse(request.getUrl().toString()));activity.startActivityForResult(i, 2001);} catch (ActivityNotFoundException ignored) {}}return true;}}
To test your app:
- Open your website integrated with standard checkout.
- Pass
webview_intent: true
parameter in the sent to checkout to enable UPI intent support.
Watch Out!
By default, the top PSP apps appear on the customer's mobile irrespective of the installation status of the UPI apps.
Is this integration guide useful?