0

I'm building an application where the user must complete a task within 5 minutes of receiving a local notification. If the task is completed then nothing happens but if the task is not completed then I need to run a parse cloud code function.

My issue is that if the user doesn't interact with the local notification I need to perform a parse cloud function. But I'm having a ton of difficulty trying to do this because of iOS's picky background modes and multitasking rules.

So far my app works great, but if the action is not completed and the user is not in the app I can't perform my code.

If anyone could point me in the right direction I would really appreciate it. If you need anymore details please let me know!

Cloud Code- This is the cloud code I would like to perform if the user does not complete the task within five minutes of reciving the local notifcation:

Parse.Cloud.define("chargeCustomer", function(request, response) {
  Stripe.Charges.create({
    amount: request.params['amount'],
    currency: "usd",
    customer: request.params['customerId']
  }, {
    success: function(customer) {
      response.success(charge.id);
    },
    error: function(error) {
      response.error("Error:" +error); 
    }
  })
});

Thanks!

Jack
  • 304
  • 6
  • 19

1 Answers1

1

Have you looked into running a Job in Cloud Code?

Say you create a new class named "PushNotification". Say you add a boolean column/property to that class that is named "handled".

When the user gets and views the PUSH notification, you could update the "PushNotification" object that corresponds to that PUSH notification and set "handled" to YES/true.

You could write a simple Job that ran every X minutes. It might look like this:

Parse.Cloud.job("checkPushes", function (request, status) {
    var PushNotification = Parse.Object.extend("pushNotification");
    var query = new Parse.Query(PushNotification);
    // Limit the query to objects created more than 5 minutes ago
    query.lessThan("createdAt", new Date(new Date().getTime() - (1000 * 60 * 5)));
    // Limit the query to objects where "actionPerformed" not equal to "true"
    query.notEqualTo("handled", true);
    query.each(function (pushNotification) {
        // Don't forget to set "handled" to true so
        // the same items don't keep popping up...
    });
});

You can find out more about Cloud Code (specifically Background Jobs) here.

mbm29414
  • 11,558
  • 6
  • 56
  • 87
  • I'm a little confused. I did my research on background jobs with parse, and it seems like a good solution. However I'm using local notifications to alert the user not push. And what would I do if the user sets the alarm to repeat everyday, every weekday, ect? I updated the question with the cloud code I would like to perform if the task is not completed. Thanks! – Jack Nov 08 '14 at 20:07
  • Any ideas? There must be a simpler way then then to run a background job. Because then I need to save the alert on the device and on parse. – Jack Nov 09 '14 at 22:50
  • How do you expect it to be possible to do this on-device if you can't be sure the user is running the app? You have to be in control of the code executing, which means Cloud Code. At least, I can't think of a way for you to control this on-device. You simply can't force the user to open your app, so you can't FORCE the code to run except in the cloud. If it's a scheduled local notification, I'd say you have to schedule it in Cloud Code and then check (after it's "5 minutes old") whether the user did something with it. Let me know if you figure out something else. – mbm29414 Nov 10 '14 at 00:55
  • @Jack Also, based on your code, it looks like you want to charge the user if they DON'T do something? I'm not sure that'll go over well, FWIW. – mbm29414 Nov 10 '14 at 00:58
  • Thanks for the reply, sounds like this is the only way. I will keep looking into and try a few things out. And yeah I want to charge the user if they don't complete a task within five minutes of receiving a local notification. Maybe this will make things more clear. Thanks! – Jack Nov 10 '14 at 01:02