Search code examples
iosobjective-cparse-platformpush-notificationparse-cloud-code

How to send push notification after a row is inserted in parse for iOS(Obj-C)


I have an Order table in parse. After a row is inserted in to the table, I want to send a push notification to the iOS device. How to write a cloud code for this specific task? I read parse documentation but I couldn't understand about channels, installation and other stuff related to push notifications. I have seen various blogs regarding this but I couldn't get a right answer for cloud code and how to call that code in iOS. Any help is appreciated. Thank you.


Solution

  • You don't say which devices you want to send the push notification to, so I can't provide a more specific example, but here is an afterSave method for a class called "Location". In this code, a query is defined that identifies all installations that are associated with the updated location and a 'silent push' is sent to these devices -

    Parse.Cloud.afterSave("Location", function(request) {
    
      query = new Parse.Query("Installation")
      query.equalTo("location",request.object)
    
      Parse.Push.send({ 
          where: query,
          data: {
              "content-available" : "1",
              event: "locationUpdate"
          } 
      },{
        success:function() {
       },
       error:function() {
           console.error("Error in push "+error.code+" : "+error.message);
           }
       });
    
    });