2
votes

I'm working on a project and I decided to use cloud code to send these user to user push notifications. So here's what I want to do:

When a new row is created/saved then take the content from the 'toUser' column which is a Pointer<_User> and then once I've got the content from the 'toUser' column I'd like to send a push notification to that username, and the usernames are available under 'username' as a string in the '_User' class.

This is the code I've tried to use:

 Parse.Cloud.afterSave("CustomMessage", function(request, response) {

      var toUser = request.object.get('toUser');

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

      Parse.Push.send({
        where: pushQuery, // Set our Installation query
        data: {
          alert: "You've got a new message from " + toUser
        }
      }, {
        success: function() {
          // Push was successful
          response.success();
        },
        error: function(error) {
          throw "Got an error " + error.code + " : " + error.message;
          response.error();
        }
      });
    });

For some reason, this code isn't working for me. It sends a push notification onto parse and it reads like this: parse push panel

Could somebody please show me how to do this?

I appreciate any help,

armanb21

1

1 Answers

2
votes

To nofity user, you need to link _Installation and _User class. To my application, I've added a user column in my _User class.

// Notify
var query = new Parse.Query(Parse.Installation);
    query.equalTo('user', request.object.get('toUser'));

    Parse.Push.send({
        'where': query,
        'data': {
            'alert': "You've got a new message from " + toUser
        }
    },
    {
        success: function() {
            // Push was successful
        },
        error: function(error) {
            // Handle error
        }
    });