I have developed a Web application using Phonegap which reads cross domain Rss feeds using Google-Feed API and sends system notification to the user if a new feed is available.
I have used this Phonegap System Notification Plugin for creating push notifications.
This is the SystemNotificaton.java that comes with the plugin.
This is the SystemNotification.js file that comes with the plugin, which has all the methods that can be used in your javascript file.
This is my notification.js javascript file were I have created two functions to read two separate Rss feed urls and I have used setIntervals to call the function every 1 min.I have also called navigator.systemNotification.onBackground(); method inside the same function so that when my app is not active this function keeps running in the background and displays the notification to the user if a new feed comes up.
The problem is I get notifications once the application starts then when i close the application I get a couple of more, after one or two days the notification stops coming. I know since I am using google-feed api it is not real time but still even after two days it does not work . It seems like the onbackground method it not working. Could anyone point out what mistake I am doing.
I don't know much about Phonegap, but I know a fair bit about Android development, and it sounds to me like the notifications aren't your problem. (Incidentally, these are just notifications, not "Push" notifications. Those are another beast altogether.)
I suspect that your application is simply stopping, and you don't have anything set to start it back up. The Android OS will kill any background application for a variety of reasons. If implemented as a service, the OS is supposed to restart it at some point, but that's not entirely reliable.
Basically, you need to set an alarm to babysit your service and make sure it stays alive. If this is possible in PhoneGap, I recommend getting rid of the setInterval altogether, and just using the alarm to wake your app up and trigger the RSS polling action. The benefit of this is that it will still work when the device goes to sleep.
Doing a bit of quick searching seems to indicate that PhoneGap does not have access to the AlarmManager. It could be done with a plugin, maybe, but PhoneGap doesn't strike me as a good framework for background services in general. A truly native app is going to be far better behaved in this context.
Related
I'm trying to create an app that manages a BLE connection in the background (receives notifications, reconnects and subscribes to a characteristic when connection is lost and the phone is moved near the BLE device again).
Right now I have the app somewhat built - everything that I need works relatively well but only when the app is open. Although the code isn't up to standard as this is my first app and I don't have any previous experience with Java/Kotlin/Dart (this is also why I'd rather not touch anything outside of flutter).
I need the app to send me local push notifications when tabbed out of the app or when screen lock is on and as far as I understand one could do this with isolates. I've searched a lot but found one guide which is kind of hard to understand as a beginner and also uses Kotlin.
Can I get away with only dart code for background code execution (described above) and is it suitable for a beginner?
Also, let me know if you have any alternative solutions since all I need is to get a notification on my phone whenever it receives a notification from a BLE device (and also auto connecting). I've already tried this with kivy since I'm more familiar with python but with no luck.
Talking about BLE there's this plugin by the way: https://pub.dev/packages/flutter_reactive_ble
As for the isolates, they are not ideal for this even though they do run separately, they'll get killed eventually. There's also a little helper package that makes working with isolates very straightforward if you want to try it: https://pub.dev/packages/computer
And so actually for the task at hand (background execution) I would suggest researching/using this one: https://pub.dev/packages/background_fetch
I would not encourage you to use any timer-based solution that tries to wake up your phone every 15 minutes or so, since that uses unnecessary CPU time.
Now I'm not that much into Flutter, but at least on Android, the "correct" way to have an ongoing connection (auto-connect) is to have a Foreground Service in the process running, so the process does not get killed. Then subscribe to characteristic notifications as usual, and handle the values as they arrive, either in Java or Flutter (by using some Java <-> Dart bridge).
I hope this question meets the criteria for the site. I'll happily update otherwise.
I am, like many others, developing an application for multiple platforms using the PhoneGap (in this case Cordova 3) framework and want to keep my application alive while the use performs others tasks. I have read around this subject and seem many opinions and possible/partial solutions.
Notionally, what I want to do is allow the user to return to the application had they pressed back, home, they got a phone call, they played tetris or whatever and specifically if the app wants them back under certain conditions.
I am presently tackling this by concentrating on the Android platform and will expand out after this solved. I understand the mechanism by which Android OS will kill processes that are not visible and understand and have implemented an example background service.
I employ JQuery Mobile with collapsible content for my application.
Before I continue development, I would like to ensure I am on the right path.
I have successfully implemented pause/resume to handle phone locking and sleep. My application does not die and I can make it beep and vibrate through appropriate plugins.
My continued intention is this ...
Write a background service to perform periodic "checks" in this case position of phone relative to a known position. The intention is to inform the user when they have reached certain proximity. This would be started on the "pause" event and stopped on the "resume" or of the app is restarted.
Using Local Storage, maintain a record of state of the UI so it can be restarted in the correct state. Upon restart, the stored state will be restored and the app will look like it never went away. There is nothing in the app which continuously updates, like graphics or logs, so there is no reason to retrieve "past states" from the background service and perform a rendering of those as if they had been performed by the app, but that would not seem unreasonable should that be required on the future.
As it's possible that the app will not be running, it seems necessary for the service to create a ("something important has happened") notification that the user can click on in order to restart the application. I have found a couple of status bar notification plugins and intend to use one. I trust I will be able to beep/vibrate accordingly from the background service alo.
In addition, a "stuff is generally happening" continuous notification could be used to restart the app. I see no conflict with the above. It would be nice to find a "bring app to foreground is it's running" method, but I'm holding out no hope.
My essential question is this ...
Am I approaching this problem correctly and maintaining as much cross platform work as possible whilst not setting myself up for a fall when it comes to implementing the iOS facet of the solution?
I just don't want to miss a trick I've not located in the sea of answers to what seems like a common problem.
I have been working on an Android app using Phonegap and now would like to make it so when the app is closed it can still execute the java/js code in the app. So I understand I need to create a service. If I create a service plugin on phonegap can I still execute the javascript code or only the java?
Has anyone does something like this? I found this discussion but did not seem to work: http://groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6
So that is all I have right now.
Before I turn to developing it native if I can't figure it out thought I would ask if anyone has done this before. I can't seem to find any of the phonegap plugins that do something similar.
EDIT: I have got an app that executes Java code as a service. However when it calls sendjavascript it does not work. So is there a way to have the javascript code running in the background as well when an app is closed with phonegap?
Thanks
No, it is not possible to run Javascript code in the background (at least in my opinion) as a service. Phonegap on Android uses an special activity called Droidgap, which hosts a WebView. This browser control executes the JavaScript. This means that JS execution can only handled inside this activity, regardless if it is visible or not.
The code you linked from Google Groups tries to bind a service developed in Java to the DroidGap activity, so the service is NOT written in JS.
You can have some background activity within your JS code inside your child activity derived from the DroidGap activity. For example have a background thread in your activity, have a JS callback function and let the thread call this callback functionality.
If you really need a service you have to go native.
Update:
JS code can only be executed with the Droidgap activity. An activity can have 3 states (based on the Lifecycle of activites):
visible
invisible but still loaded
not loaded
I provided a sample in which I implemented a Phonegap plugin. The plugin allows the activity to register itself to SMS_RECEIVED. When the activies goes out of scope (event onbeforeunload), it deregisters, so only issue 1 is handled.
When you want all 3 issues handled, you have to forward the incoming SMS intent to the activity. When it is not loaded the system will automatically load and activate the activity. But this is not a background service anymore, your app would become visible whenever a SMS is received.
If you don't want this (if you really want a background service), you have to provide a native implementation.
There is this article on how to create a service on Android with Phonegap which gives some good information on your problem.
It's using a great plugin in order to build a background service with phonegap easily. But you can't use JS though
I didn't find a way to make JS to run in the Background. BUT you can pass parameters from Java to JS and vice versa with the plugin...which is pretty useful.
You would still need to rewrite your JS code in Java though.
Unless you do have a specific reason to only want JS to be run? (But there shouldn't be...)
Hope that could be useful to some people visiting this page.
YES, and it is very simple... just install the plugin backgroundJS:
https://build.phonegap.com/plugins/430
It allows you to run javascript on the background and combined with the local notification plugin, you can even send notifications to the user at any time, just keep in mind that doing this will cause the battery to run out faster, also consider that this might create a problem with the iOS policy. good luck!!!
You can try to add plugin cordova-plugin-background-mode
But as author says:
Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed.
Use the plugin by your own risk!
I am trying to run a snippet of javascript which keeps polling the server for updates. what is the best way to get this working with minimal impact on application performance from the end user perspective.
I tried spawning it off in a separate iframe but the resultant performance was pretty bad.
I am currently working on android so if the solution involves writing a phonegap plugin to poll the server instead of javascript that should be fine for now. I need to update data in local storage after polling the server.
I have implemented this in IOS. The problem with phonegap is that you can't create a timer task that runs javascript because if your .html page changes the task would be killed.
Unless you just have ONE .html and you are dynamically changing the body part. If that is the case then look up setInterval in javascript.
If that isn't the case then implementing something on javascript isn't a solution, in IOS I have implemented a custom plugin for running asynchronous web service calls. You could do something similar, writing a plugin that runs a AsyncTask doInBackground() and then postback the results in the javascript. There are many tutorials on how to create a custom plugin in phonegap. Hope it helps
If you are planning to poll only when the application is in use the setInterval() works fairly well. My application is using jQuery Mobile which by default only updates DOM rather reloading entire page, that way my setInterval() keeps running once started.
Android already has a support of Background services, which you can use for the long and continuous running tasks.
One solution which I have used with above combination:
when the app is running I use setInterval() to check for update periodically.
Background checks whether application is running or not before starting the update checks
If the app is not running then I use the background service code just to check whether there are any updates on server or not
If there are updates then I put a system tray notification to notify user that there are updates waiting for him
As soon as he starts the application, I call the service to fetch the updates.
This way my main logic remains in the Javascript side and I need not to write same logic in both Javascript and native again.
If updates are not very frequent then you should consider using PushNotification
I have been working on an Android app using Phonegap and now would like to make it so when the app is closed it can still execute the java/js code in the app. So I understand I need to create a service. If I create a service plugin on phonegap can I still execute the javascript code or only the java?
Has anyone does something like this? I found this discussion but did not seem to work: http://groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6
So that is all I have right now.
Before I turn to developing it native if I can't figure it out thought I would ask if anyone has done this before. I can't seem to find any of the phonegap plugins that do something similar.
EDIT: I have got an app that executes Java code as a service. However when it calls sendjavascript it does not work. So is there a way to have the javascript code running in the background as well when an app is closed with phonegap?
Thanks
No, it is not possible to run Javascript code in the background (at least in my opinion) as a service. Phonegap on Android uses an special activity called Droidgap, which hosts a WebView. This browser control executes the JavaScript. This means that JS execution can only handled inside this activity, regardless if it is visible or not.
The code you linked from Google Groups tries to bind a service developed in Java to the DroidGap activity, so the service is NOT written in JS.
You can have some background activity within your JS code inside your child activity derived from the DroidGap activity. For example have a background thread in your activity, have a JS callback function and let the thread call this callback functionality.
If you really need a service you have to go native.
Update:
JS code can only be executed with the Droidgap activity. An activity can have 3 states (based on the Lifecycle of activites):
visible
invisible but still loaded
not loaded
I provided a sample in which I implemented a Phonegap plugin. The plugin allows the activity to register itself to SMS_RECEIVED. When the activies goes out of scope (event onbeforeunload), it deregisters, so only issue 1 is handled.
When you want all 3 issues handled, you have to forward the incoming SMS intent to the activity. When it is not loaded the system will automatically load and activate the activity. But this is not a background service anymore, your app would become visible whenever a SMS is received.
If you don't want this (if you really want a background service), you have to provide a native implementation.
There is this article on how to create a service on Android with Phonegap which gives some good information on your problem.
It's using a great plugin in order to build a background service with phonegap easily. But you can't use JS though
I didn't find a way to make JS to run in the Background. BUT you can pass parameters from Java to JS and vice versa with the plugin...which is pretty useful.
You would still need to rewrite your JS code in Java though.
Unless you do have a specific reason to only want JS to be run? (But there shouldn't be...)
Hope that could be useful to some people visiting this page.
YES, and it is very simple... just install the plugin backgroundJS:
https://build.phonegap.com/plugins/430
It allows you to run javascript on the background and combined with the local notification plugin, you can even send notifications to the user at any time, just keep in mind that doing this will cause the battery to run out faster, also consider that this might create a problem with the iOS policy. good luck!!!
You can try to add plugin cordova-plugin-background-mode
But as author says:
Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed.
Use the plugin by your own risk!