简体   繁体   English

Apache Cordova:从Web视图引发事件

[英]Apache Cordova: raising events from the web view

new apache cordova dev here. 这里有新的Apache Cordova开发人员。

I have a very simple apache cordova app that is simply a shell for my web project. 我有一个非常简单的Apache Cordova应用程序,仅是我的Web项目的外壳。

In the onDeviceReady event I just go very simple and do: 在onDeviceReady事件中,我非常简单地执行以下操作:

var url = "http://www.app.com:3000/users/sign_in";

The app is working. 该应用程序正在运行。 Now I wanted my web app to raise notifications in the phone, so I imagined that I could raise an HTML event from my web app to the cordova app and then have the cordova app fire the native notification to the phone using a plugin. 现在,我想让我的Web应用程序在手机中引发通知,因此我想可以将HTML事件从我的Web应用程序引发到Cordova应用程序,然后让Cordova应用程序使用插件将本地通知激发到手机。

This is nice because the web does not have to include any cordova files, i keep it detached from the cordova tech. 这很好,因为Web不必包含任何cordova文件,因此我将其与cordova技术分离。 It seems like a nice perk. 看起来不错。

So I'm trying to dispatch an event from the web: 因此,我正在尝试从网络发送事件:

var customEvent = new CustomEvent("notification", { detail: { message: message, options: options }, bubbles: true });
document.dispatchEvent(customEvent);

And binding it on the cordova app.initialize: 并将其绑定到cordova app.initialize上:

document.addEventListener('notification', this.onNotification.bind(this), false);

But, when I bubble the event from the web, the cordova app is not receiving it. 但是,当我从网络上冒泡该事件时,cordova应用程序未收到该事件。 Upon further research, I found that the document object in the cordova app does not seem to be exactly the same than the document object in my web app. 经过进一步研究,我发现cordova应用程序中的文档对象似乎与Web应用程序中的文档对象不完全相同。

My presumption was that this was very common, but I looked around and I couldn't find many people doing this. 我的推测是这很普遍,但我环顾四周,找不到很多人这样做。 Is this possible (bubbling events from the web app to the cordova app)? 这可能吗(将事件从Web应用程序冒泡到cordova应用程序)? if not, what is the best way for my web app to communicate with my cordova app? 如果不是,我的Web应用程序与Cordova应用程序进行通信的最佳方式是什么?

I discovered that, when redirecting the local window using window.location.href, you lose the local DOM and basically everything that you do in the app is ignored. 我发现,当使用window.location.href重定向本地窗口时,您将丢失本地DOM,并且基本上在应用程序中执行的所有操作都将被忽略。

Starting to build on that, I added the InAppBrowser plugin and opened my web app as a _blank window and configured it so it looks full screen. 开始以此为基础,我添加了InAppBrowser插件,并以_blank窗口的形式打开了我的Web应用程序,并对其进行了配置,使其看起来全屏。

    webWindow = cordova.InAppBrowser.open(url, '_blank', 'location=no,zoom=no');

After that, I found that it is not possible to bubble events from the web app to the cordova app. 之后,我发现不可能将事件从Web应用程序冒泡到cordova应用程序。 I tried using addEventListener and dispatching events from the web but I could not make it work. 我尝试使用addEventListener并从Web调度事件,但是我无法使其工作。

So I ended hacking my way through it: I used the evaluateScript method on the webWindow object. 因此,我结束了自己的研究工作:我在webWindow对象上使用了validateScript方法。 Using evaluateScript I set and retrieve a variable on the window object in the web. 使用评估脚本,我在Web的窗口对象上设置和检索变量。 Every 5 seconds or so I poll the variable and if there's something on it, I pull the value and clean the variable. 每隔约5秒钟,我会轮询一次变量,如果变量上有内容,我会拉取值并清理变量。

On the web side, I fill that everytime a new notification comes. 在网络上,每次收到新通知时,我都会填写。

It is sub-optimal but it's the best I could do. 这不是最理想的,但这是我能做的最好的。 It carries the huge advantage of having a very thin cordova app that I can practically forget about it. 它拥有一个非常薄的cordova应用程序的巨大优势,我几乎可以忘记它。 It is mobile for free. 它是免费的移动设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM