Search code examples
iosparse-platformios8uilocalnotificationparse-cloud-code

iOS Detect If User Doesn't Interact With Local Notification


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!


Solution

  • 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.