2

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

Community
  • 1
  • 1
armanb21
  • 53
  • 7

1 Answers1

2

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
        }
    });
Bluety
  • 1,869
  • 14
  • 22
  • what do you mean by linking _Installation and the _User class? In my user class I have a username string? Is that fine? Thanks for your help, armanb21. – armanb21 Mar 28 '15 at 21:26
  • Hi @Bluety, this hasn't worked. My code is [here] (https://gist.github.com/armanb21/997fe0a53483f37ca189) I followed your instructions. Although, I'm not sure about the linking part. The notification is on Parse but never got sent on the iPhone. I do get this error on the console: 2015-03-28 21:58:33.787 Happy Day[22794:814640] Error: no results matched the query (Code: 101, Version: 1.4.1) .Could you please help me as this is something I've been wanting to do for ages. I'd really appreciate it. Thanks, armanb21. – armanb21 Mar 28 '15 at 21:56
  • To link the _Installation and _User class, I recommend you to use a pointer rather than the username string. – Bluety Mar 29 '15 at 13:21
  • To save a user pointer: `{'__type': 'Pointer', 'className': '_User', 'objectId': XXXXX}`. You can replace username by a pointer – Bluety Mar 29 '15 at 13:50
  • in the CustomMessages class where the messages are saved I have a user column pointing to _User and a toUser column also pointing to _User. The toUser column determines the person who'll recieve the message and the user column determines who sent the message. The string, username is actually in another class, _User. If I replaced the username string in _User as a Pointer, what would I point it to? Is their a tutorial I could follow or instructions that make it more clear as I'm new to Parse. Thanks for your help! – armanb21 Mar 29 '15 at 14:14
  • If you want to send a push notification to a user, you should know what installation correspond to this user. You must add column 'user' in your _Installation class. When a new customMessages are posted, you querying the _Installation class with column 'user' equalTo 'toUser'. (sorry for my english). – Bluety Mar 29 '15 at 14:24
  • When you save Installation entry you must register the user. After that it will be simple to notify users – Bluety Mar 29 '15 at 16:36
  • Hi @Bluety : for some reason, now the push notifications aren't sending to parse either. For cloud code, I did this: http://bit.ly/1a7aWFK (links to github gist) and I do save the http://bit.ly/1NrvMff (links to image) and I saved a message to the CustomMessage class but it doesn't show in the Parse push panel at all. Could you please help me? Thanks for your help so far. – armanb21 Mar 29 '15 at 17:00
  • You var 'toUser' in your alert is not defined. – Bluety Mar 29 '15 at 17:26
  • Thank you so,so,so much! It works! Have a nice day, armanb21. – armanb21 Mar 29 '15 at 19:09
  • Nice! You can check my answer. – Bluety Mar 29 '15 at 20:17
  • I earned it and gave it to you. – armanb21 Mar 29 '15 at 20:48
  • Thank you, but I just talked to mark the answer as "accepted". http://stackoverflow.com/tour. Good luck for your app – Bluety Mar 29 '15 at 21:00