0
votes

I'm having trouble targeting the push notifications to a single user. Right now my test device is receiving a push notification no matter which user is logged into the device. Even when I send a push to _User that is currently logged out and the device is logged in with a completely different _User it receives the push notification.

I have set up cloud code with parse.com to enable push notifications to be sent to users. I have a "user" pointer in the installation class that points to the cooresponding _User that is to receive the push notification.

Example: _User "Sam" is logged in to the mobile device. I send a push from the iOS simulator in Xcode to _User "steve". The mobile device receives the push even though "Sam" is logged in. It receives the push even if NO user is logged in.

When I check the Push control on parse, it seems like the cloud cloud is correctly querying the "user" pointer in the installation that matches the objectId for the _User class, as seen here:

user advanced operator ($inQuery {"className"=>"_User", "objectId"=>"LJQXaJDzkU"}) 
SENDING TIME
August 5th, 2015 at 3:01 PM

EXPIRATION
None

FULL TARGET
{ 
  "user": { 
    "$inQuery": { 
      "className": "_User", 
      "objectId": "LJQXaJDzkU" 
    } 
  } 
}

FULL DATA

This is the cloud code that queries the user and sends the push from the parse cloud. The recipientUserId receives a string of the objectId for the _User whom is receiving the push.

Parse.Cloud.define("sendPush", function(request, response) {
	var senderUser = request.user
	var recipientUserId = request.params.recipientId;
	var senderUserId = request.params.senderId;
    var message = senderUserId + " sent a message"
  
  	var recipientUser = new Parse.User();
	recipientUser.id = recipientUserId;
	var pushQuery = new Parse.Query(Parse.Installation);
	pushQuery.matchesQuery('user', recipientUser);
	
	Parse.Push.send ({
		where: pushQuery,
		data: {
			aps: {
				alert: message,
				badge: "Increment",
				sound: "cow-moo1.wav"
			}
		}
	}, {
		success: function() {
		response.success("Success!");
	}, 
		error: function(error) {
		response.error(error);
	}
	
	});
	
});

The pushes send no problem, and I can open them on the device but I'm really lost as to why the pushes are being sent to the device even when the queried user isn't even logged in to the device. Any ideas as to whats going on would be greatly appreciated. Thanks!!!

1

1 Answers

1
votes

Edit your code so that when you log out, you're removing the user from the installation.

Edit - This was mentioned in the comments below, so I want to make sure it was mentioned here in case it was the solution that worked. OP used matchesQuery, which requires a query to be passed in, but was passing in a Parse.User object instead. I suggested he used equalTo instead of matchesQuery.