Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data from my TWA webapp to my app?

With Crosswalk I had a very convenient javascript-to-app interface so I could call a java function from javascript and share data from my webapp to my android app.

How can I achieve this with Custom Tabs (or Trusted Web Activity) ?

There seems to be no way at all. There should be, especially when my app and my game/webapp are from the same author.

For example, I do not trust LocalStorage, especially now with Custom Tabs, it may get cleaned, or the user may uninstall the browser and install another one, so the saved data will be lost and the user will be angry at the app for the loss of the saved data, not even understanding that the data were in the browser, not in the app. So I used to have my webapps call the app to save datas.

Another example, when the Custom Tab uses Firefox instead of Chrome, then speech synthesis won't be available. I can detect it easily in my webapp. But I want my webapp to call the app and send it the words to pronounce. That is what I was doing with Crosswalk since it didn't support speech neither.

I understand that webviews are more appropriate for my use than Custom Tabs, but when the webview can't be used on a device (especially Android <5) then my app doesn't have a lot of other options than opening a Custom Tab instead (or Trusted Web Activity if available). I can't use Crosswalk anymore, it is discontinued and still full of serious bugs. And other solutions such as GeckoView or Alibaba Gcanvas are not ready.

edit:

In this article about Trusted Web Activity https://developers.google.com/web/updates/2017/10/using-twa I read

Nevertheless, you can coordinate with the web content by passing data to and from the page in URLs (e.g. through query parameters, custom HTTP headers, and intent URIs.)

edit:

I've been reading many pages, Intents and deep-linking are still obscure to me though, but here is what I tried.

I added an intent filter for a custom action :

<receiver android:name=".OutgoingReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="custom_tabs_js_interface" />
    </intent-filter>
</receiver>

I created a class for that receiver :

public class OutgoingReceiver extends BroadcastReceiver {

    public static final String CUSTOM_INTENT = "custom_tabs_js_interface";

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "received" , Toast.LENGTH_SHORT).show();
    }

and I call it in javascript with

location.href="intent:#Intent;action=custom_tabs_js_interface;end";

I don't even pass data for now, I just try to call it. but nothing happens...

like image 242
Diego Avatar asked Aug 27 '18 15:08

Diego


People also ask

What is PWA TWA?

Trusted Web Activity (TWA) and Progressive Web Applications (PWA) are connected. A Trusted Web Activity (TWA) is merely an Android app but does not need to create it. All we need to do is launch the required PWA site into the Android app.

What is TWA in Web?

Trusted Web Activity is a new solution to open web-app content in apps like Progressive Web App (PWA) and uses a protocol based on Custom Tabs. Chrome 72 and above versions for Android support TWA. TWA displays a fullscreen browser in your Android app and allows you to use all the features and services of that browser.

What is TWA in Android?

Trusted Web Activities (or TWA) — A complete implementation guide to OYO Lite. We all know that users like to keep only those apps which they use on a regular basis. The primary reason for uninstalls is the size of the app.

What trusted web activities?

A Trusted Web Activity runs Chrome, in full screen, inside an Android application, without any browser UI visible. This a powerful capability and is only allowed when the site being opened and the Android application belong to the same developer. This validation is achieved through Digital AssetLinks.


1 Answers

Yes broadcast receiver doesn't work for some reason, probably security. But you can use an Activity in place of Broadcast Receiver to do this as below.

Use custom Android Intent Uri with custom host, query parameters, package, scheme and intent action. Invoke this intent uri from your javascript/html code. Example

"intent://myhost?key=value#Intent;scheme=myscheme;package=my.app.package;action=someaction;end"

Also declare an activity in the manifest file with intent filter to handle the specific host, scheme & action. Example

<activity android:name="my.app.package.ReceiverActivity">
    <intent-filter>
        <action android:name="someaction" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="myhost"
            android:scheme="myscheme" />
    </intent-filter>

</activity>

Now in the activity, handle the intent and extract the data from query parameters. Do whatever you want with the data and probably finish the activity in case you want to go back to the same screen. Example

public class ReceiverActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String value = getIntent().getData().getQueryParameter("key");
        if (value != null) {
            // value is basically your data
        }

        // in case you want to go back to same screen
        finish();
    }
}

And that is it. You have data for your disposal at an Android Activity. This ReceiverActivity could (preferrably) belong to same TWA app. Now from this Receiver Activity, you can easily send/share the data to any other apps. Hope this helps.

like image 192
shanx Avatar answered Oct 01 '22 03:10

shanx