Reputation: 610
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.
Upvotes: 39
Views: 66122
Reputation: 93
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.
Upvotes: 0
Reputation: 2964
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.
Upvotes: 29