0

I have a Class "Storybaord" and there are two instances of when to send a Push Notification.

  1. A user likes a post - [postObject addUniqueObject: [PFUser currentUser] forKey:@"likes"];

  2. A user comments on a post - [postObject addUniqueObject: self.comment forKey:@"comments"];

In my cloud code I use Parse.Cloud.afterSave but I am unsure of how to distinguish between the two, and also determine if they even happen, because there are other occasions of saving the postObject without needing to send a push.

Cloud Code:

Parse.Cloud.afterSave("Storyboard", function(request){
    var user = Parse.User.current();
    var postUser = request.object.get('userId');

    var pushQuery = new Parse.Query(Parse.Installation);
    pushQuery.equalTo ('userId', postUser);

    Parse.Push.send({
            where: pushQuery,
            data: {
                alert: "Liked Your Post"
                }
                    }, {
                    success: function(){

                    },
                    error: function(){

                    }
            });
    });
8
  • So your using client-side code to perform the object updates but cloud code to send a push? You will have to tell if client-side to perform a cloud function with parameters for the push or simply push the notification client-side. Either way is done within the postObject client-side. Would you like an example answer for a client side or are you dead-set on cloud push? Commented Aug 28, 2015 at 1:31
  • @soulshined Well right now push notifications are sent client side, but from what i have read, it is a security issue. So, I am trying to switch over to cloud code to send push notifications. Commented Aug 28, 2015 at 1:35
  • So then, create a cloud code function for when a user likes a postObject and if successfully saved, send a push. Then create a separate cloud code function for the comment and send a push within that function. Essentially just like you would client side. No reason to create a conditional statement when it can all be handled in responses or call backs Commented Aug 28, 2015 at 1:37
  • @soulshined Yes understood. Thats what I'm trying to do but this is my first time writing cloud code and am confused what to call to distinguish which is which. Commented Aug 28, 2015 at 1:42
  • @soulshined something with Parse.Op.AddUnique()? Commented Aug 28, 2015 at 1:42

1 Answer 1

1

If your insistent on sending the push via cloud code, all you have to do is set a conditional statement client-side and act accordingly, that way you can opt to send or not according to your needs, since you mentioned something about selectively sending pushing:

if (self.likeButton.isSelected) {
    [PFCloud callFunctionInBackground:@"alertAuthor" withParameters:@{@"message", [NSString stringWithFormat:@"%@ liked your post!", [PFUser currentUser].username]}
} 

if (self.commentEntered) {
    [PFCloud callFunctionInBackground:@"alertAuthor" withParameters:@{@"message", [NSString stringWithFormat:@"%@ commented on your post!", [PFUser currentUser].username]}
}

And simply send a push using the parameters via cloud code:

Parse.Cloud.define("alertAuthor", function(request,response){
  var query = new Parse.Query(Parse.User);
  var message = request.params.message;
  query.equalTo('username', 'postUser');

  Parse.Push.send({
    where: query,
    data : { 
      alert: message,
      badge: "Increment",
      sound: "",
    }
    }, {
    success: function() {
    //Success
    },
    error: function(error) {
    //Oops
    }
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.