Parse Cloud Code: can you pass objects in a push notification?

I am creating a push reminder similar to this sample code provided by Parse:

Parse.Cloud.afterSave('Activity', function(request) { if (request.object.get("type") === ("comment") { var message = request.user.get('displayName') + ': '; message += request.object.get('content').trim(); var query = new Parse.Query(Parse.Installation); query.equalTo('user', request.object.get("toUser")); Parse.Push.send({ where:query, data: { alert: message, badge: 'Increment' } }); } }); 

My question is: in the Parse.Push.send data area, can I send the entire message object, where is the Message user class that I created? If so, what will it look like?

+4
source share
3 answers

If you already have a class created and saved object, why not just send the identifier of the object and request it asynchronously after the user goes to the push notification?

The message object does not need to waste space and send it via push, only a pointer is required.

I am doing something similar and this is the route that I plan to use.

+1
source

You can serialize the object in JSON / XML format and then deserialize it when you receive a push notification.

+1
source

You cannot send objects directly. You will get an exception (I had this problem a few days ago, but I did not write the name of the exception). The best answer so far is BrentonGray88.

I assume you are not using Android because you turned on the icon: the value is "Increment", but I would do this:

Android code to send notification:

Make sure the Comment object has a pointer (_User) column for the user who posted the comment. When you create a comment, include user.put("commentAuthor", ParseUser.getCurrentUser()); into your Android code so that you can always access the user who created the comment.

Now you need to request a comment in order to send it to the object via a push notification.

 ParseQuery<ParseObject> query = new ParseQuery<>("Comment"); query.whereEqualTo("objectId", I AM NOT SURE WHAT CONDITION YOU WANT HERE); query.findInBackground((comment, e) -> { if (e == null) for (ParseObject commentObject: comment) { String recipientObjectId = commentObject.getParseObject("commentAuthor").getObjectId(); final Map<String, Object> params = new HashMap<>(); // This is to send the notification to the author of the Comment params.put("recipientObjectId", recipientObjectId); // This is so we can use values from the Comment in the notification params.put("commentObjectId", commentObject.getObjectId()); // This is a required lined params.put("useMasterKey", true); ParseCloud.callFunctionInBackground("pushMessage", params, new FunctionCallback<String>() { public void done(String result, ParseException e) { if (e == null) { Log.d(getClass().toString(), "ANNOUNCEMENT SUCCESS"); } else { System.out.println(e); Log.d(getClass().toString(), "ANNOUNCEMENT FAILURE"); } } }); } }); 

Now for the request in your Cloude codec:

  Parse.Cloud.define("pushMessage", function (request, response) { // Again, we are sending the notification to the author of the Comment var pushQuery = new Parse.Query(Parse.Installation); pushQuery.equalTo('user', request.params.get("recipientObjectId")); // We retrieve information from the Comment created by that author var commentQuery = new Parse.Query(Parse.Comment); commentQuery.equalTo('objectId', request.params.commentObjectId); commentQuery.get("commentObjectId", { success: function(userObject) { var displayName = userObject.get("displayName"); var content = userObject.get("content"); var message = displayName + ': '; message += content.trim(); Parse.Push.send({ where: pushQuery, data: { alert: message }, }, { useMasterKey: true, success: function () { response.success("Success!"); }, error: function (error) { response.error("Error! " + error.message); } }); console.log(); }, error: function(error) { console.log("An error occured :("); } }); }); 

Sorry I'm not the best in JavaScript, but the way I would do it. Good luck! :)

0
source

Source: https://habr.com/ru/post/1499711/


All Articles