40

what I mean is working in totally background, e.g. even the screen is shut down, the app is running and can send notifications with a sound.

My app is used for watching price changes. There will be an alert with a sound when a price changes.

So, the answer should be yes or no? Thanks.

Leo Cai
  • 612
  • 1
  • 7
  • 13

3 Answers3

47

Here is some links which can help you in this

link 1 : Similar SO question

link 2 : Red-Folder Blog

link 3 : BackgroundService Plugin in GitHub

Update

link 4 : Phonegap background service on iOS4

link 5 : phonegap background service in iOS5

Community
  • 1
  • 1
Melvin Joseph Mani
  • 1,494
  • 15
  • 25
  • wait. All 3 links provide solution to Android. How about other PhoneGap supported mobile operating system, such as iOS & Blackberry OS? – Raptor Jan 09 '13 at 06:46
  • 21
    Links are no good answers, as they might get weak. you should post a brief summary of whats in the link. – brainray May 08 '15 at 12:06
29

Since there came another possible solution with iOS 7, I'm gonna provide an additional answer for users of iOS 7 and further.

The new Background Fetch feature, makes it possible to regularly update content for an app which is in the background. The time interval of the fetching cannot be set by the user, but is rather set by the iOS based on its user's statistics (app usage, etc.).

This new feature can be accessed with PhoneGap/Cordova via plugins - fortunately there has already been developed a plugin providing this access. You can install it to your Cordova project by

cordova plugin add https://github.com/christocracy/cordova-plugin-background-fetch.git

In conjunction with a plugin providing access to iOS's local notifications, this works wonders. Such a plugin has also been developed, for example this one. Install it to your Cordova project by

cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications.git

These plugins can now be combined in your javascript code, to execute background activities:

function onDeviceReady() {
    var Fetcher = window.plugins.backgroundFetch;

    // Your background-fetch handler.
    var fetchCallback = function() {
        console.log('BackgroundFetch initiated');

        // perform your ajax request to server here
        $.get({
            url: '/heartbeat.json',
            callback: function(response) {
                // process your response and whatnot.

                window.plugin.notification.local.add({ message: 'Just fetched!' });  //local notification
                Fetcher.finish();   // <-- N.B. You MUST called #finish so that native-side can signal completion of the background-thread to the os.
            }
        });
    }
    Fetcher.configure(fetchCallback);
}

This fetching plugin is using the UIApplicationBackgroundFetchIntervalMinimum value for the fetching interval, this results in the fastest possible fetching periodicity.

Clawish
  • 2,934
  • 3
  • 24
  • 28
  • 4
    And what about Android? Why most plugins are written for a single platform instead of the main 3 platforms? – andreszs Dec 29 '14 at 02:11
  • @Andrew I guess the answer to your question is that each of the platforms deal with background processes in entirely different ways and creating a single plugin would be rather tiresome and require a breadth of knowledge. I'm sure it will come, I want it too! – Nathan Edwards Mar 21 '15 at 08:23
  • Android users should use [this plugin](https://github.com/katzer/cordova-plugin-background-mode). I heard that using it on iOS will probably get your app rejected in the review process if you're not using it for geolocation, etc. – Clawish May 15 '15 at 09:09
0

If you building cordova for ios 7 +, and are prepared to step out of your generic code into xcode, you can add 'required background modes' to the .plist file of your ios build and it won't be overwritten by a new build.

e.g. I chose the 'App downloads content from the network' option

All you then have to do is make sure your app pokes the outside world every few minutes (I am using firebase, so I get the value of a dummy node).

I've not submitted to app store yet, but can't see how it should fail if the request is not too frequent (I understand apps have about 10 mins in background before they are suspended) and the resources that your app needs in background/at rest are not onerous.

This gets the resolution of this issue down to a couple of lines in your code.

blythburgh
  • 93
  • 5