Installation Query using an inner Query on Parse User class to send Push Notifications

192 views
Skip to first unread message

m...@maxtannone.com

unread,
Sep 13, 2016, 9:13:25 PM9/13/16
to back{4}app
I am trying to use Advanced Targeting (using advanced targeting) to send some notifications via Cloud Code.

This is my current function:

Parse.Cloud.define('sendKeywordNotifications', function (request, response) {
    // THIS METHOD NO LONGER WORKS
    // Parse.Cloud.useMasterKey();


var itemSearchStringArray = request.params.itemSearchStringArray;
var fullMessage = request.params.fullMessage;
var objectIdOfUserWhoPostedTheitem = request.params.objectIdOfUserWhoPostedTheItem;
console.log("XXXXXXXX");
console.log(itemSearchStringArray);
console.log("about to send some notifications");
var currentUserPointer = Parse.User.createWithoutData(objectIdOfUserWhoPostedTheitem);
console.log(currentUserPointer);

var userQuery = new Parse.Query(Parse.User);
userQuery.notEqualTo("objectId", objectIdOfUserWhoPostedTheitem);
userQuery.notEqualTo("following", currentUserPointer);
userQuery.equalTo("notificationWhenFriendPostsNewItem", true);
userQuery.containedIn("notificationsArray", itemSearchStringArray);
console.log(userQuery);
console.log("----------");
var installationQuery = new Parse.Query(Parse.Installation);
installationQuery.matchesQuery("user", userQuery);
console.log(installationQuery);
console.log("----------");

    Parse.Push.send({
 where: installationQuery,
 data: {
alert: fullMessage,
sound: "default"
 }
}, {
useMasterKey: true,
 success: function() {
console.log("the push was successful");
// Push was successful
 },
 error: function(error) {
console.log(error);
// Handle error
 }
});

response.success("overall function successful");
});


However, my pushes are not sending. This is the output of my various console.log statements:

XXXXXXXX
[ 'test', 'chair', 'hello' ]
about to send some notifications
ParseUser { _objCount: 176, className: '_User', id: 'LUbRhX9iY1' }
ParseQuery {
  className: '_Installation',
  _where: { user: { '$inQuery': [Object] } },
  _include: [],
  _limit: -1,
  _skip: 0,
  _extraOptions: {} }
ParseQuery {
  className: '_User',
  _where: 
   { objectId: { '$ne': 'LUbRhX9iY1' },
     following: { '$ne': [Object] },
     notificationWhenFriendPostsNewItem: true,
     notificationsArray: { '$in': [Object] } },
  _include: [],
  _limit: -1,
  _skip: 0,
  _extraOptions: {} }
----------
----------
the push was successful


My only guess is that something is wrong with this output:

  { following: { '$nin': [Object] },
notificationsArray: { '$in': [Object] } },

It looks weird that it says "Object" and not the actual object that is supposed to be there.

Lastly, I noticed that my console.log() statements are sometimes out of order from how they are written. I don't even see how?



m...@maxtannone.com

unread,
Sep 13, 2016, 9:29:54 PM9/13/16
to back{4}app
I wanted to add that my User has the keyword "chair" in their `notificationsArray`. `chair` is the keyword I have been testing with.

Davi Macêdo

unread,
Sep 14, 2016, 9:53:35 PM9/14/16
to back{4}app
Hi, Max.

I've checked your Installation class and the column that has the pointer to the User class is not called user, but userObjectId.

Therefore you have to change this line here:
installationQuery.matchesQuery("user", userQuery);

To this one here:
installationQuery.matchesQuery("userObjectId", userQuery);

You have another problem in your code (probably the one that is generating the logs in "wrong" order). Instead of:
success: function() {
console.log("the push was successful");
// Push was successful
  },
  error: function(error) {
console.log(error);
// Handle error
  }
});

response.success("overall function successful");

You have to call response function in the callbacks:
success: function() {
console.log("the push was successful");
// Push was successful
response.success("overall function successful");
  },
  error: function(error) {
console.log(error);
// Handle error
response.error("error: " + error);
  }
});

Try again and let me know.

best!

Max Tannone

unread,
Sep 24, 2016, 1:41:11 AM9/24/16
to back{4}app
Hey Davi,

Thanks for responding. I haven't been able to work on this because I was migrating the project to Swift 3. Now that is done, I started working on this issue again.

In my User class, I have both a "userObjectId" string and a "user" pointer to the User class.

Even putting aside that query constraint for a moment (I've commented it out below), my log statements are still appearing out of order.

This is the current state of my main.js file. I suspect I have something wrong with the response.success/error statements...



Parse.Cloud.define('sendKeywordNotifications', function (request, response) {
    // THIS METHOD NO LONGER WORKS
    // Parse.Cloud.useMasterKey();


var itemSearchStringArray = request.params.itemSearchStringArray;
var fullMessage = request.params.fullMessage;
var objectIdOfUserWhoPostedTheitem = request.params.objectIdOfUserWhoPostedTheItem;
console.log("XXXXXXXX");
console.log("about to print the itemSearchStringArray");
console.log(itemSearchStringArray);
var currentUserPointer = Parse.User.createWithoutData(objectIdOfUserWhoPostedTheitem);
console.log("about to print the currentUserPointer");
console.log(currentUserPointer);

var userQuery = new Parse.Query(Parse.User);
userQuery.notEqualTo("objectId", objectIdOfUserWhoPostedTheitem);
//userQuery.notEqualTo("following", currentUserPointer);
userQuery.equalTo("notificationWhenFriendPostsNewItem", true);
userQuery.containedIn("notificationsArray", itemSearchStringArray);
console.log("about to print the userQuery");
console.log(userQuery);
console.log("----------");
var installationQuery = new Parse.Query(Parse.Installation);
installationQuery.matchesQuery("user", userQuery);
console.log("about to print the installationQuery");
console.log(installationQuery);
console.log("----------");

    Parse.Push.send({
 where: installationQuery,
 data: {
alert: fullMessage,
sound: "default"
 }
}, {
useMasterKey: true,
 success: function() {
console.log("the push was successful");
// Push was successful
response.success("overall function successful");
 },
 error: function(error) {
console.log(error);
// Handle error
response.error("error: " + error);
 }
});

//response.success("overall function successful");
});


Once I get the console.log statements printing out in the correct order, I want to re-visit the issue of comparing the User pointer that I am creating. (the inner query original question...)

Additionally, I don't know if I am receiving this statement because of the incorrect order, but I am getting this output when I try to send a notification:

ERR! cannot find vaild connection for ### <-- (i replaced the long number here, not sure if privacy issue...)
parse-server-push-adapter APNS 




Max Tannone

unread,
Sep 24, 2016, 1:45:10 AM9/24/16
to back{4}app
I wanted to add that the console is printing:

the push was successful

Max Tannone

unread,
Sep 24, 2016, 5:28:54 PM9/24/16
to back{4}app
Please ignore this issue:

ERR! cannot find vaild connection for ### <-- (i replaced the long number here, not sure if privacy issue...)
parse-server-push-adapter APNS 

I had a messed up push certificate. However, my console.log statements are still appearing out of order. Any help is greatly appreciated on any of this.

Davi Macêdo

unread,
Sep 24, 2016, 11:39:37 PM9/24/16
to back{4}app
Hi, Max. What do you mean by out of order? Could you please paste here how it is printing now? Best.

Max Tannone

unread,
Sep 25, 2016, 1:45:58 PM9/25/16
to back{4}app
Hey Davi, this is an example of the output. If you compare this output to my sendKeywordNotifications function in my main.js file (or the same function I pasted above), you will see the print statements are out of sequence....Which I think is related to my inner-query problem:

XXXXXXXX
[ 'sets', 'drawers' ]
about to print the itemSearchStringArray
about to print the userQuery
ParseUser { _objCount: 150, className: '_User', id: 'LUbRhX9iY1' }
about to print the currentUserPointer
ParseQuery {
  className: '_Installation',
  _where: { user: { '$inQuery': [Object] } },
  _include: [],
  _limit: -1,
  _skip: 0,
  _extraOptions: {} }
----------
about to print the installationQuery
ParseQuery {
  className: '_User',
  _where: 
   { objectId: { '$ne': 'LUbRhX9iY1' },
     notificationWhenFriendPostsNewItem: true,
     notificationsArray: { '$in': [Object] } },
  _include: [],
  _limit: -1,
  _skip: 0,
  _extraOptions: {} }
----------
the push was successful

Davi Macêdo

unread,
Sep 25, 2016, 6:50:19 PM9/25/16
to back{4}app
Hi, Max.

The our of order problem was regarding a bug in the logs ordering. Just solved it. Can you try again pls?

Best.

Max Tannone

unread,
Sep 25, 2016, 7:37:59 PM9/25/16
to back{4}app
Thanks Davi. My issue appears to have been solved!
Reply all
Reply to author
Forward
0 new messages