The reason the embedded webpage cannot send messages to the application is because the embedded webpage does not have a link to the application.
top.postMessage does not apply to the application. top will work if you are trying to access the topmost frame within a single web view.
To send messages to the application, the web page needs a link to the application. The easiest way to do this is to force the application to send the first message to the frame - a hello message.
From the app:
// Initialize communications webView.contentWindow.postMessage('hello, webpage!', 'https://your.web.page/*'); addEventListener('message', function(e) { // I expect this check to work, but I have not tested it. if (e.source != webView.contentWindow) return; // Handle e.data however you want. });
On the web page:
var messageSource, messageOrigin; addEventListener('message', function(e) { if (!messageSource) { if (e.data == "hello, webpage!") { messageSource = e.source; messageOrigin = e.origin; messageSource.postMessage("hello, host!", messageOrigin); } } else {
source share