Home > Enterprise >  share a link by whatsapp that opens a specific application instead of browser
share a link by whatsapp that opens a specific application instead of browser

Time:12-13

I have an app programmed in java for android with a range of devices from sdk 23 to 33.

This app only incorporates a webview where I load my web page/app https://tcg-wallet.ga

a brief explanation of what the site does: it does web scraping to obtain TCG card price data and everything is referenced to a search url volatile.

Inside the website I have added an html button with the intention of opening the share dialog using javascript:


const shareData = {
    title: 'Search Price',
    text: 'Search Card by: "'   document.getElementById('search').value   '"',
    url: document.URL
}

const btn = document.getElementById('share-url');

if (typeof btn !== 'undefined') {
    btn.addEventListener('click', async () => {
        try {
            console.log(window.AndroidShareHandler);
            if (typeof (window.AndroidShareHandler) != 'undefined') {
                window.AndroidShareHandler.shareSearch(JSON.stringify(shareData));
            } else {
                alert('shared Working');
                await navigator.share(shareData);
            }
        } catch (err) {
            console.log(err);
            alert('[Error]: Error on share link!');
        }
    });
}

and i received it on the app on android via:

webview.addJavascriptInterface(new JavaScriptShareInterface(context),"AndroidShareHandler");

I successfully tried to share this data like this to other apps:

@JavascriptInterface
public void shareSearch(String javascript_data) {
    try {

        JSONObject search = new JSONObject(javascript_data);
        String title = search.getString("title");
        String text = search.getString("text");
        String url = search.getString("url");
        String content = title "\n" text "\n\n" url;

        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setType("text/plain");
        sendIntent.putExtra(Intent.EXTRA_TEXT, content);
        app_context.startActivity(Intent.createChooser(sendIntent, content));

    } catch (Exception ex) {

    }
}

but the result is not as desired (the problem)

When I share the information, for example through WhatsApp, the message must be sent as plain text and by including a url like this (https://tcg-wallet.ga/home?search=MP21-EN056&event=search) if they click on it link, it opens automatically in the android web browser and not in my application.

Question: How can I register in the android operating system or configuration that when trying to open the url my application opens and that I pass the url arguments to it.

update it is my intent-filter:

<intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

CodePudding user response:

I think you should declare an intent-filter on your AndroidManifest.xml, something like that:

<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="tcg-wallet.ga"
                android:scheme="https" />
        </intent-filter>

Inside the Activity you want to manage the url

CodePudding user response:

After a while of research I have found what I needed to implement certainly:

  1. Edit AndroidManifest.xml: you must include new intent-filters, place them after the filter that brings the application by default, like this:
             <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data
                    android:scheme="https"
                    android:host="tcg-wallet.ga" />
            </intent-filter>
            <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:scheme="app"
                    android:host="com.icarosnet.tcgwallet" />
            </intent-filter>
  1. then you must make sure in your MainActivity.java to evaluate if you are receiving the intent:
        Uri url_shared = getIntent().getData();

        if(url_shared != null){
            Url = url_shared.toString();
        }

once the url variable has been processed it will contain the full web address.

  1. At this point we have already done the main part of the development or integration of the intent reading, but even so the application will not open automatically: for this we will need to enable the validation of the intent host:

  2. our intent-filter must contain the self-verification instruction android:autoVerify="true" and on our website we must have a digital signature resource json file that links to the application:

https://your-host-domain/.well-know/assetlinks.json
or
http://your-host-domain/.well-know/assetlinks.json

with json content like:

[
  {
    "relation": [
      "delegate_permission/common.handle_all_urls"
    ],
    "target": {
      "namespace": "package_namespace",
      "package_name": "com.package_name.myapp",
      "sha256_cert_fingerprints": [
       "14:6D:E9:83:C5:73:06:50:D8:EE:B9:95:2F:34:FC:64:16:A0:83:42:E6:1D:BE:A8:8A:04:96:B2:3F:CF:44:E5"
      ]
    }
  }
]

Relationed documentation: Link-1 and Link-2

  • Related