PhoneGap的 - 处理推送通知,一旦我离开了索引范围与window.location.replace索引、离开了、范围、通知

2023-09-05 06:00:44 作者:Bury.(埋葬)

我使用的PhoneGap 的4.2(基于科尔多瓦5.0)来创建一个跨平台的应用程序。

I'm using Phonegap 4.2 (based on Cordova 5.0) to create a cross-platform application.

该应用程序是为Android和iOS。

The application is for Android and iOS.

当用户加载应用程序,他位于 index.html的。从那里,我有机会获得各种JavaScript方法,raw-,科尔多瓦 - 基 - 和我碰巧使用 jQuery的 v1.11.1。

When a user loads the app, he is located in index.html. From there I have access to various JavaScript methods, raw-, Cordova-based- and I happen to be using jQuery v1.11.1.

当应用程序收到一个推送通知的应用程序必须能够访问位置的有效载荷,并从那里访问专门提到目标属性(例如'的 http://stackoverflow.com/questions/ask )。我做了。然后用户被要么被重定向到特定的位置,这是一个网站(不打开一个浏览器,的即的。离开该应用),或的以包括网站在索引文件中并替换它的内容。

When the app receives a push notification the app must be able to access a location property in the payload and from there access the specifically mentioned destination (e.g. 'http://stackoverflow.com/questions/ask'). I'm doing that. Then the user is either to be be redirected to that specific location, which is a website (without opening a browser, i.e. leaving the app), or to include the website within the index file and replace it's content.

使用的iFrame 似乎并没有成为一种选择,简单地包含在索引文件中的网站,由于网站只是显示为空白或其他一些问题 - 虽然我可能是错的,但是这是的排序的题外话(可能会解决整个问题寿,我都快要present)。

Using iFrames did not seem to be an option, to simply include the website within the index file, due to the website simply appearing as blank or some other issues -- although I might be wrong, but that's sort of off topic (would probably fix the whole issue tho that I'm about to present).

阅读一些文章(后 antonylees ,Stackoverflow,等),我来到了单纯用 window.location.replace(externalUrl)的解决方案; 进入该网站。它很好地工作。 (请注意,网站建设,并包装成我的应用程序是不是一种选择,因为它太重了)。

After reading some articles (antonylees, Stackoverflow, etc) I came down to the solution of simply using window.location.replace(externalUrl); to access the site. It work perfectly. (Note that building the site and wrapping it into my app is not an option as it's too heavy).

到目前为止,一切都很好

我使用的是 PushPlugin 科尔多瓦插件来处理推送通知。一切都建立和运作;我也收到推送通知。

I'm using the PushPlugin Cordova plugin to handle Push Notifications. Everything is set up and works; I do receive push notifications.

要能够处理(即的过程)推送通知,我已经注册了 onNotification 事件侦听器,无论是Android和iOS。

To be able to handle (i.e. process) the push-notifications I have registered onNotification event listeners, both for Android and iOS.

有三种方案处理一个到达的推送通知的时候,我必须考虑到:

There are three scenarios that I must take into account when processing an arriving push notification:

一键到达→用户presses通知→应用程序是从冷启动(应用程序没有运行)&#x2192开始;有效载荷处理与索引文件中的`onNotification`可行的方法,用户被重定向到的推送通知的有效载荷指定位置 一键到达→用户presses通知→应用程序被启动,而在背景(即运行),但用户还没有离开索引文件→有效载荷处理与索引文件中的`onNotification`可行的方法,用户被重定向到的推送通知的有效载荷指定位置 一键到达→用户presses通知→应用程序被启动,而在背景(即运行),但是用户已经离开了索引文件,通过被已经重定向过去的网站,并已不再访问`onNotification`方法,因为它不会在存在新的网站→用户停留的确切位置,他并没有被重定向到一个新的位置 Push arrives → User presses the notification → App is started from a coldstart (the app wasn't running) → The payload is processed with the `onNotification` method available in the index file and the user is redirected to location specified in the push notification's payload Push arrives → User presses the notification → App is started while in background (i.e. running), but the user has not yet left the index file → The payload is processed with the `onNotification` method available in the index file and the user is redirected to location specified in the push notification's payload Push arrives → User presses the notification → App is started while in background (i.e. running), but the user has already left the index file, by being already redirected in the past to the website, and has no longer access the `onNotification` method because it doesn't exist in the new website → User stays exactly where he was and has not been redirected to a new location

我的问题

方案1和2的工作很好,因为我有机会获得附加的通知监听器,它们可以处理负载,实现了我送的有效负载的位置属性,以确定用户重定向到。

Scenario 1. and 2. work fine because I have access to the attached notification listeners, and they can process the payload and achieve a location property that I send with the payload, to determine where the user is to be redirected to.

在方案3,然而,用户已经重定向出索引文件(和外部网站),并在技术上指数的范围,因此从现在起我不再有权访问,在分别提供的方法索引文件,包括 onNotification 的方法。

In Scenario 3, however, the user has been redirected out of the index file (and to an external website) and technically the index's scope, so from now on I do no longer have access to the methods that were available in the index file, including the onNotification methods.

由于这个原因,我不能将用户重定向到的时候他已经收到推送通知新的位置。

Due to this, I can not redirect the user to a new location when he has received a push notification.

所以我的问题是:

我怎么可以处理新到的推送通知的有效载荷,使索引文件后?有没有可能是更好的方法简单地包括在索引中的网站,从而使previously定义的方法可以使用​​,每当我需要他们?

How can I process the newly arrived push notification payload, after leaving the index file? Are there perhaps better alternatives to simply include the website within the index, and thus making the previously defined methods available to use whenever I need them?

感谢你。

修改

有人向我指出的InAppBrowser插件可以提供我在寻找什么,提供一个回调函数或code注入到浏览器中。我会尽力今晚回来一份报告,在此期间,我倒是AP preciate从,如果你发生如此回答有关于该情况有更清楚的线索:)

It has been pointed out to me that the InAppBrowser plugin might offer what I'm looking for by providing a callback function or code injection into the browser. I'll try it tonight and come back with a report, in the meantime I'd appreciate answers from SO if you happen to have a better clue about the situation :)

推荐答案

使用的 InAppBrowser 的。开(URL,_blank,参数)办法解决我的问题。

Using the InAppBrowser's .open(url, '_blank', args) method solved my issue.

新加载的页面能够访问previously定义的方法,我还能够注入一些code,并根据听众添加一些事件到新分配的页面,如果我感兴趣的是,但是这不是必需的。

The newly loaded page is able to access the previously defined methods and I'm also able to inject some code and add some event based listeners to the newly assigned page, if I'm interested in that, but that isn't required at all.