Poramate Homprakob
Poramate Homprakob

Reputation: 11

How to run task in background for making notification every minute in swift

My app is about checking every minute then make notifications if my database is updated.

I have finished my app in an Android version that has a simple workflow like using BroadcastReceiver to start a repeating AlarmManager on the device boot, and then let it do the task every minute.

My trouble when making the iOS version are how to start on the device boot and how to let it do the task background every minute.

Upvotes: 1

Views: 702

Answers (2)

Lukesivi
Lukesivi

Reputation: 2226

EDIT Parse.com is dead. The answer below was valid when it was written back when.

Parse Shutdown Reminder The hosted Parse service is now shut down. To all the developers out there, it was a pleasure helping you build apps. Goodbye and good luck!

If you're using Parse you can check every minute if the database was updated through Cloud Code and then set a push notification based on that.

For a quick answer:

First, you have to set up Parse Cloud Code to do this however. https://parse.com/docs/cloudcode/guide here are the docs for Cloud Code.

Here are the push notification docs: https://www.parse.com/docs/ios/guide#push-notifications

Longer answer:

This is how I did it:

https://parse.com/apps/quickstart#cloud_code/unix use this quick-start to help you out.

  1. You're going to have to write some javascript logic for what you're trying to execute as the tutorial/docs explain.

For example, in this code I want the posts that are older than 24 hours to be deleted:

// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
Parse.Cloud.define("hello", function(request, response) {
  response.success("Hello world!");
});
Parse.Cloud.job('deleteOldPosts', function(request, status) {

                    // All access
                    Parse.Cloud.useMasterKey();

                    var today = new Date();
                    var days = 1;
                    var time = (days * 24 * 3600 * 1000);
                    var expirationDate = new Date(today.getTime() - (time));

                    var query = new Parse.Query("ProductInfo");
                        query.lessThan('createdAt', expirationDate);

                        query.find().then(function (ProductInfo) {
                                                      Parse.Object.destroyAll(ProductInfo, {
                                                                                              success: function() {
                                                                                                  status.success('All posts are removed.');
                                                                                              },
                                                                                              error: function(error) {
                                                                                                  status.error('Error, posts are not removed.');
                                                                                              }
                                                                                          });
                                                  }, function (error) {});

                }); 
  1. Once you have the javascript logic for checking if the database was updated, go to your project, then click Core and then click Jobs.

  2. Then, schedule a job.

You'll get something like this:

enter image description here

Where it says "Minutes" you can choose 1 minute.

Hope that helps.

PS, this is a similar question I answered a few weeks ago that may help: Swift2 push when app is closed

Upvotes: 0

Mateusz
Mateusz

Reputation: 1224

Fortunately it is impossible in iOS. There is no possibility to run programmatically after device boot also working the app in background is really limited.

Upvotes: 1

Related Questions