2

Tried loads of different variations with my cloud code and I can't get it to work. Basically I've got a push notification function, and in this function I want to add an object to a PFUser's array, but you can't use a master key in Xcode so here's what I have:

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

console.log("Inside iOSPush");

var data            =   request.params.data;
var not_class       =   request.params.not_class;
var not_objectid    =   request.params.not_objectid;
var not_date        =   request.params.not_date;
var userid          =   request.params.userid;
var recipientUser   =   new Parse.Query(Parse.User);
recipientUser.equalTo("objectId", userid);

//  set installation query:

var pushQuery       =   new Parse.Query(Parse.Installation);
pushQuery.equalTo('deviceType', 'ios');
pushQuery.matchesQuery('user', recipientUser);
pushQuery.find({ useMasterKey: true }).then(function(object) {
    response.success(object);
    console.log("pushQuery got " + object.length);
}, function(error) {
    response.error(error);
    console.error("pushQuery find failed. error = " + error.message);
});

//  send push notification query:

Parse.Push.send({
    where: pushQuery,
    data: data
}, { useMasterKey: true }).then(function() {

    console.log("### push sent!");

    //  create notification:
    var notification = {
        "title": not_class,
        "body": request.params.data.alert,
        "class": not_class,
        "objectId": not_objectid,
        "date": not_date
    };

    //  get notifications:
    var tmp_notifications = recipientUser.get("notifications");

    //  add notification:
    tmp_notifications.push(notification);

    //  update with notifications:
    recipientUser.set("notifications", tmp_notifications);
    recipientUser.save();

}, function(error) {
    console.error("### push error" + error.message);
});

response.success('success. end of iospush');

});

The Xcode cloud function I have provides the correct information, the function gets to the end.. just the function is not setting the notifications for some reason

1 Answer 1

3

I ended up figuring out the answer to this post myself. The reason this didn't work is because I needed to first fetch the user object in a separate query, then save it using the master key. I also found out that there's a function for appending data onto an existing array without having to create another one (parseObject.add()):

var userQ   =   new Parse.Query(Parse.User);
userQ.get(userid, {
    success: function(theuser) {
        console.log("### got userrrrrrrrrr!");
        theuser.add("notifications", n_object);
        theuser.save(null, {useMasterKey:true});
    },
    error: function(object, error) {
        // The object was not retrieved successfully.
        // error is a Parse.Error with an error code and message.
    }
});

This set of code was executed just before:

response.success('success. end of iospush');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.