Hook a Javascript event to fire on another application OR suggestions - javascript

We have a web based jquery mobile app that records activity of a user. This app is long standing and in use at the moment, so re-writing the application is not an option unfortunatly.
The problem is that we are trying to get GPS location at certain points using the application.. But as it is running in an instance of chrome, the javascript will not record correctly when the phone is locked, or the chrome is minimised.
So to get around that we are writing an Ionic App with Cordova in order to get the GPS coordinates in the background, regardless of the state of the browser.
Currently, this is working great. The issue however is there are certain events in our javascript that require a specific 'type' associated to our GPS logging.
My question is: How do I actually hook onto this javascript event in CHROME from the GPS background ionic/cordova application.
What we have tried so far:
Finding the chrome localstorage via Ionic/Cordova and reading a value from that on a timer.
The issue with this is we can't find the location of the localstorage/cache.
From this question it says its here:
/data/data/com.android.chrome/cache
but we can't find/access it from our ionic app or file browser on the android
https://android.stackexchange.com/questions/85998/android-google-chrome-browser-cache-location
We have also looked for ways to hook the javascript events to fire something inside the other application but we are having no luck there either..
Apologies for the long post.. Its hard to explain. If you have any suggestions on alternative methods to do this kind of cross-application event firing from Chrome to Ionic/Cordova I would be greatly interested to hear your oppinion.

To fix this I created a 'stack' where I could push an object to an array that would be picked up by the cordova app using executeScript as follows:
In the inappbrowser site:
var stackName = stackN1
function addEventToStack(numid, type) {
var stackRow = { 'numb': numid, 'type': type};
var stack = localGet(stackName); //localstorage
if (!stack)
stack = StackInit();
stack.push(stackRow);
localStore(stackName, stack);
console.log('Added to stack ', stack);
return 'Row Added';
}
In the cordova application:
ref.executeScript(
{
code: 'localStorage.getItem("stackN1")'
},
function(rdObj) {
//Iterate and use rdObj here
}

Related

Google maps javascript api crashes in phonegap [Hard topic]

I'm facing problems when the client internet connection is unstable. If disconnections occur during the loading process google maps services won't work even when the connection comes back.
The difficulty with phonegap compared to a normal browser is that there is no "reload page" button the user can hit if the page didn't load properly. We thus have to ensure a 100% safe load.
If you follow the instructions provided by Google to implement google maps javascript api in your phonegap app, the loading process will crash at three different steps if the client connection is unstable.
Each crash is independent and a bit complex to explain, so i created sub questions : first crash, second crash, third crash
I also created that file so that anyone can reproduce the crashes.
I suspect that the problem partly comes from google's script, but there is probably a work around i didn't see.
Not sure at all that this could help but I faced an issue regarding multiDex when I tryed to use google map api when adding this code in file build-extras.gradle my google map crash was solved :
android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
multiDexEnabled true
}
dexOptions {
javaMaxHeapSize "2g"
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
}
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
}
What I understood was that using simple Dex I can only implement 65000 method but the google api add lot of them.
Building Apps with Over 65K Methods
Not sure this could help or not, let me know if I should delete this answer.

IOS Cordova Push Plugin - Coldstart Crashes App

I am using the the Cordova Push Plugin: http://ngcordova.com/docs/plugins/pushNotifications/
This works fine in Android Platform. But, for IOS, I face the following issue:
I register listener for '$cordovaPush:notificationReceived' event as per the documentation and provide the same implementation as given in the documentation in the link above (given below for ease):
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
if (notification.alert) {
navigator.notification.alert(notification.alert);
}
if (notification.sound) {
var snd = new Media(event.sound);
snd.play();
}
if (notification.badge) {
$cordovaPush.setBadgeNumber(notification.badge).then(function(result) {
// Success!
}, function(err) {
// An error occurred. Show a message to the user
});
}
});
There are 3 scenarios:
1. App is running in foreground. In such case, even though the notification arrives (confirmed by log statements), no visible action happens on the device.
I expected the below two statements to execute but they dont.
navigator.notification.alert(notification.alert);
snd.play();
App is running in background. In such case, the statements seem to execute as per expected behaviour.
App is NOT running at all (coldstart). In this case, the notification and sound are played but when user click on notification, the app opens and hangs / crashes.
Has anyone encountered these problems before? What is the best way to solve these? This is only for IOS.
The plugin you are using is deprecated.
i also used it before and there are many issues.
i would reccomend to use the plugin: phonegap-plugin-push
easy to install and will solve your issue
As mentioned by #Nechemya Kanelsky, use the newer version of the push plugin and scenario 1 and 2 will be handled. But with that plugin as well, the 3rd issue still remains, as mentioned here
You can use the fix for 3rd issue, mentioned here

Lawnchair storage with 2 collections on Phonegap Android

I ran into a nasty problem with Lawnchair (0.6.1). I want to have 2 globally accessible collections/Lawnchairs, both with the webkit-sqlite adapter. This is how I declare them, right after loading Lawnchair and the adapter:
var clientStore = Lawnchair({name: 'clients'}, function () {});
var companyStore = Lawnchair({name: 'companies'}, function () {});
// other global variables
...
In Safari and Chrome on my desktop everything is smooth sailing, same for my iPad and other iOS devices in the emulator using Phonegap (3.0.0). No problems at all, both databases show up in the web inspector and in Weinre. I can use them in any of JQuery Mobile events (version 1.3.2 and 1.4.0 beta). Manually checking the database objects in Weinre confirms that they are indeed using the webkit-sqlite adapter.
But if run the Phonegap app on Android, only the clientStore is accessible and usable. Trying to access the companyStore gives me the following error:
companyStore.all(function (allEntries) {
console.log(allEntries);
...
> TypeError: Cannot call method 'all' of undefined
The only database that shows up in Weinre on the Android device is the clients database. Manually initializing the companies database from the Weinre inspector console after everything loaded up doesn't show any effect on the list of databases on the 'Resources' tab or change anything else (except for the above error visually disappearing).
Trying to access the 'undefined' companyStore programmatically, say when someone switches to a page where all the companies shall be listed, 'crashes' the UI on the Android device. Nothing even gets printed on the console anymore after that. As said, all other platforms work just fine.
Am I just doing something stupidly wrong or did I encounter a bug/feature?
Thanks a ton for the help, this one is driving me up the wall :(.
Edit: using the standard DOM adapter works for me on every platform so far. I guess I'll stick to that one for now. In case someone ran into the same problem, here's how to explicitly use the DOM adapter:
var clientStore = Lawnchair({name: 'clients', adapter: 'dom'}, function () {});
var companyStore = Lawnchair({name: 'companies', adapter: 'dom'}, function () {});

titanium webview fireEvent addEventListener

here's my situation. I have a html +css + jquery well working project that I want to adapt in titanium. This project has geolocation + fb api call.
I want to adapt my project into a titanium html5 project. What I found is that I can call titanium api only through addEventListener and fireEvent functions (of course only if I use webviews).
it' my first titanium project I work with that needs geolocation and facebook api.
actually, I started to modify the previous project by adding addEventlistener into the app.js file and fireEvents into the javascript files of the previous project ( included in the first project in the html files) in the parts that need the titanium api calls (I can't call titanium api outside of app.js).
the problem is that I need some values (objects) to be returned back.
to better understand what I'm doing, here's the sequence of the events.
TITANIUM PROJECT
(app.js)
var win = Ti.UI.createWindow();
var webview = Ti.UI.createWebView({
url: 'index.html'
});
Ti.App.addEventListener('geolocation',function(){
//some titanium api call
lat = x;lon=y;
Ti.App.fireEvent('geolocation_back',{latitude:lat,longitude:lon});
});
win.add(webview);
win.open();
HTML + CSS + JS PROJECT
(imported file into index.html, not imported into app.js)
Ti.App.fireEvent('geolocation');
var my_lat ;
var my_lon ;
Ti.App.addEventListener('geolocation_back',function(d){
my_lat = d.latitude;
my_lon = d.longitude;
//do other stuff with my_lat and my_lon
});
I hope you understand what I'm doing.
my questions are:
1) is what I am doing the correct way to work with titanium and html code?
2) is there anyother way to call titanium api within html code and return variables/objects back?
EDIT
this code works only on iOS and android but not on web browser. it seems that the built in server (Titanium studio or Android web browser emulator) doesn't load the Ti.* or Titanium.* objects. is there anyway to make it works on web browser?
I see the web mobile compiler creates all the titanium API in subfolders
there is titanium.js and TI/* folder. can anyone explains me why the console says me Ti is not defined?
as I said here
I found a solution!
simply add to all of your html pages the simple script below
var Ti = window.parent.Ti
have fun!

PhoneGap application not .live('tap') events on iOS

I have an application that is using jquery, jquerymobile and spine.js running on phonegap (0.9.5.1) and have been having some issues getting it to work properly on iOS.
The application should be launching the camera when a div is tapped. In my controller I have it so that it does something similar to the following:
myController = Spine.Controller.create({
events: {"tap .take-picture": "takePic"},
takePic: function(){
var self = this;
navigator.camera.getPicture(function(data){
self.doStuffWith(data);
},
null,
{quality: 50, destinationType: Camera.DestinationType.DATA_URL, sourceType: Camera.PictureSourceType.CAMERA})
},
doStuffWith: function(data){
// Doing stuff with said data
}
});
What is really confusing me, is that this code works properly on Android. Are there some kind of iOS quirks that make it so that tap events aren't sent off properly?
I think that you are trying to use the Android phonegap js within the iPhone app. You need to make sure that you are including the right phonegap.js for the platform you are developing. Although they share the same name, each version of phonegap is tailored to its host OS.
This could be several things:
You are testing this in the iOS Simulator. There is no Camera in the Simulator, you don't have a fail callback specified, but there is a bug (I believe) in the API where it doesn't call the fail callback if the source type is not available anyway. You should see this ("Source Type Not Available") in the Run Log (Cmd-Shift-R).
On a device, I tested your code separately, and ran it in deviceReady(), it runs - so the API call seems to be correct. I added a touch handler to a button to call the code also, so it appears tap events are working. So based on these tests (on a device):
(a) the API call works
(b) tap events work
Which leads me to the conclusion that the bug is outside of those two possibilities.

Categories