Pulkit Aggarwal
Pulkit Aggarwal

Reputation: 79

Two way bridge between native android and progressive web apps in trusted web application

As per current understanding from TWA documentation, we can pass message between native android and web using URL intents and query parameters/custom headers.

I want to pass information from web to native and return response based on the action to that message in native to web on the same page. I can do this by reopening another trusted web activity. But this leads to page load and destroys the existing state of the page.

In other words, I am trying to replicate a request-response type of behaviour in TWA without page transition on web.

Sample use case : From PWA, I want to call android native code to print a string. In case of failure cases, I want to send back the error response to PWA.

Is there a way this can be done without reloading the page?

Upvotes: 1

Views: 1847

Answers (2)

RandomDude244
RandomDude244

Reputation: 49

Starting with Chrome 115, TWAs can utilize postMessage to communicate between the web and native app at runtime.

See the official docs here: https://developer.chrome.com/docs/android/post-message-twa

I suggested reading the comments in this GitHub issue, starting with the announcement comment, as others seem to have issues getting it to work.

Upvotes: 0

andreban
andreban

Reputation: 4976

Trusted Web Activities don't have a runtime communication bridge between Android and the web app, so the short answer to the question is that this cannot be done without reloading the page.

There's a long discussion thread here: https://github.com/GoogleChrome/android-browser-helper/issues/55. Please, do chime in and describe your use case.

Upvotes: 1

Related Questions