Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap - handling push notification once I've left the index scope with window.location.replace

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

The application is for Android and iOS.

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.

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. 'https://stackoverflow.com/questions/ask'). I'm doing that. Then the user is either to 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.

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).

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).

So far, so good

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

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:

  1. 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
  2. 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
  3. 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

My issue

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.

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.

So my question is:

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?

Thank you.

Edit

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 :)

like image 922
Jonast92 Avatar asked Aug 20 '15 13:08

Jonast92


1 Answers

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

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.

like image 115
Jonast92 Avatar answered Sep 21 '22 00:09

Jonast92