question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

postMessage support?

See original GitHub issue

I noticed the APIs in CustomTabService for using postMessage, but I couldn’t get them to work. And after some searching around I found this patch, suggesting that postMessage is disabled in TWAs:

https://chromium.googlesource.com/chromium/src/+/20891520570f780b13ae9725d9aa5a909338d0a9^!/

But it was back in 2018, so I wanted to check: can I use the native postMessage APIs with TWAs? If not, are there any other strategies for transferring data between the native and web sides?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
FluorescentHallucinogencommented, Dec 1, 2020

@andreban I need to implement background geolocation in my TWA. Since background geolocation is currently not supported in Web, I plan to implement it natively. But I need to somehow pass geolocation coordinates from native code to JavaScript code every X seconds. The first idea that came to my mind is to pass geolocation coordinates via URL query parameters. But looks like it’s not a good idea, because changing URL query parameters leads to page reload. Right? Moreover, actually I need to pass the data into service worker, not into the page (because this should work in the background). What is the best way to pass data from native code to JS code in my case? Any ideas and code samples will be appreciated.

0reactions
lokariocommented, Mar 15, 2022

I have a similar use case.

I have a background service implemented in the native android project, so every now and then I want to send data from native code into javascript.

@PEConn @andreban do you think this could be done by using Web Share API?

I tried triggering an “ACTION_SEND” intent on the native side, and registering on the javascript as if it was a Web Share Target, but it didn’t work (maybe I didn’t get the idea).

I’m following these articles/tutorials: https://developers.google.com/web/android/trusted-web-activity/web-share-target https://web.dev/web-share-target/ https://developer.android.com/training/sharing/send#send-text-content

This is the android code (within service), it has Notification Delegation enabled:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.setData(Uri.parse("https://<myappdomain>.com/share-target/"));
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Some text");
sharingIntent.putExtra(Intent.EXTRA_TITLE, "Some title");
startActivity(sharingIntent);

On the web project I added this into manifest.json:

  "share_target": {
    "name": "NotificationListener",
    "action": "/share-target/",
    "method": "POST",
    "enctype": "text/plain",
    "params": {
      "title": "title",
      "text": "text",
      "url": "url"
    }
  },

and on javascript side:

self.addEventListener('fetch', (event) => {
  event.waitUntil(self.registration.showNotification('Code invoked by native'));
});

but the fetch is never called.

@AlissonRS Did you find a way to get it to work?

Read more comments on GitHub >

github_iconTop Results From Across the Web

"postMessage" | Can I use... Support tables for HTML5, CSS3 ...
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Read more >
Window.postMessage() - Web APIs | MDN
The window.postMessage() method safely enables cross-origin communication between Window objects; e.g., between a page and a pop-up that it ...
Read more >
What browsers support the window.postMessage call now?
All latest browsers supports that e.g. IE 11, Edge, Firefox 57+, Dafari 11+, iOS Safari 10.2 ...
Read more >
Using JavaScript and window.postMessage() - In Plain English
window.postMessage() is a safe way to send messages between windows in different domains or origins. One can also post to an IFrame. The...
Read more >
Attacking and Defending postMessage in HTML5 Websites
The postMessage mechanism in HTML5 enables Web content from different origins to communicate with each other, thus relaxing the same origin policy.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found