Christos Thb
Christos Thb

Reputation: 27

Parse cloud function send JSON to specific user

I create an App that a user can send messages(notifications) in other users. Fot these perpose i use parse SDK. So i send the message from device into the parse cloud with below code.

final ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.whereEqualTo("email", "[email protected]");
    query.getFirstInBackground(new GetCallback<ParseUser>() {

        public void done(ParseUser object, ParseException e) {
            if (e == null) {
                Toast.makeText(getActivity(), "User found", Toast.LENGTH_SHORT).show();
                String search_username = object.getString("username");
                String id = object.getObjectId();
                Log.d("ObjectID:",id);
                HashMap<String, Object> params = new HashMap<String, Object>();
                params.put("recipientId", id);
                params.put("message", username);
                ParseCloud.callFunctionInBackground("sendPushToUser", params, new FunctionCallback<String>() {
                    public void done(String success, ParseException e) {
                        if (e == null) {
                            // Push sent successfully
                            Toast.makeText(getActivity(), "Request send", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

Then i have the next cloud function for recieve and push the message to the specific user.

Parse.Cloud.define("sendPushToUser", function(request,response){

    var senderUser = request.user;
    var recipientUserId = request.params.recipientId;
    var message = request.params.message;
    var title ="Friend Request";
    if(message.length > 140){
            message = message.substring(0, 137) + "...";
    }

    var recipientUser = new Parse.User();
    recipientUser.id = recipientUserId;
    var pushQuery = new Parse.Query(Parse.Installation);
    pushQuery.equalTo("user", recipientUser);
    Parse.Push.send({
            where: pushQuery,
            data: {
                    "alert":{"data":{"message":"message",
                                    "title":"title"}}
                    }
    }).then(function(){
            response.success("true")
    }, function(error) {
            response.error("Push failed to send with error: "+error.message);
    });

});

But the message never been received. If i sent a push notification from parse dashboard everything works fine. Anyone knows how to solve it? The device expect a JSON to received so may my cloud function didnt send data in json format? Thanks in advance

Upvotes: 1

Views: 620

Answers (1)

SaNtoRiaN
SaNtoRiaN

Reputation: 2202

I had problems while sending notifications because of 2 things

  • enabling client push "not in your case"
  • didn't save the user in the installation "try the following"

After the user logs into your app add his id to the installation by

ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.addUnique("userId", currentUser.getObjectId());
installation.saveInBackground(new SaveCallback() {
    @Override
    public void done(ParseException e) {
        // check the returned exception
        if (e == null) {
           // everything worked fine
        } else {
            // error occurred
        }
    }
});

hope it helps :)

Update
In your code you're sending the recipient userId although you saved the username also in your cloud function you have the same problem, the username is saved but you query the installation based on the id. I've updated the installation above also change the "user" in your cloud function to the "userId"

pushQuery.equalTo("userId", recipientUser);

Upvotes: 1

Related Questions