Is there a way to open "whatsapp://send/?text=abc" links from cordova hybrid app without using any cordova social sharing plugins?

Viewed 1155

I made an hybrid app from my existing angular project using cordova. I am trying to add social sharing buttons like WhatsApp, LinkedIn, etc.

LinkedIn works fine because the share URL can be opened from InAppBrowser.

When I am trying to open whatsapp with a custom URL scheme whatsapp://send?text=abc, I get this error ERR_UNKNOWN_URL_SCHEME.

From related StackOverflow questions, I tried some solutions. None of them seems to work and I get the same error message.

I tried adding these lines in config.xml to allow whatsapp url scheme

<allow-intent href="whatsapp:*" />
<access origin="whatsapp:*" launch-external="yes" />
<preference name="AllowedSchemes" value="whatsapp" />

I tried to edit InAppBrowser.java file and modified ShouldOverrideUrlLoading method to open Intent Action View on visiting "whatsapp:".

@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
    if (url.startsWith("geo:") || url.startsWith(WebView.SCHEME_MAILTO) || url.startsWith("market:") || url.startsWith("intent:") || url.startsWith("whatsapp:")) {
        try {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    cordova.getActivity().startActivity(intent);
                    return true;
        } catch (android.content.ActivityNotFoundException e) {
                    LOG.e(LOG_TAG, "Error with " + url + ": " + e.toString());
        }
    }
}

I am using Angular 7, cordova-android 8.1.0, cordova-plugin-inappbrowser 2.0.2

To reproduce the above problem,

In html:

<button (click)="sendViaWhatsapp()">Send on WhatsApp</button>

In JS:

sendViaWhatsapp() {
    window.open("whatsapp://send/?text=abc", "_system");
}

Is it possible to achieve this without using any cordova social share plugins?

My guess is, it is an issue with Cordova WebView and the url is being opened with InAppBrowser. I am not able to find any leads on this.

Am I doing it right or Is there something that I am missing?

0 Answers
Related