1

UPDATE: I was getting this error because of a bug that Parse-Server 2.2.17 has. I fixed it by going back to 2.2.16.

Does anyone know why I am getting this error? Here is my cloud code:

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

var pushQuery = new Parse.Query(Parse.Installation);

Parse.Push.send({
    where: pushQuery,
    data: {
    alert: "New Event Added",
    sound: "default"
    }
    },{
    success: function(){
        console.log("Push Sent!")
    },
    error: function (error) {
        console.log(error)
    },
  useMasterKey: true

}); });`

Here is the error I am getting: enter image description here

And then this is how I am calling the code: `PFCloud.callFunctionInBackground("Messages", withParameters: nil) { (object, error) in

            if error == nil {

                print("Success!")

            } else {

                print(error)
            }
        }

index.js: enter image description here `

2 Answers 2

1

Can you please try the following code:

var query = new Parse.Query(Parse.Installation);
// query condition (where equal to .. etc.)

var payload = {
    alert: "New Event Added",
    sound: "default" 
};





Parse.Push.send({
    where: query, // Set our Installation query
    data: payload
}, {
        success: function () {

        },
        error: function (error) {
            // Handle error
        }
    });

Please notice that i remove the useMasterKey if you want to add useMasterKey you need to insert it inside closures but for me it's works without the useMasterKey

the useMasterKeyVersion should look like the following:

Parse.Push.send({
    where: query, // Set our Installation query
    data: payload
},
{
   useMasterKey: true
},
{
    success: function () {

    },
    error: function (error) {
        // Handle error
    }
});

The promises version (according to best practices):

Parse.Push.send({where: query,data: payload})
.then(function(){
    // success
},function(error){
    // error .. 
});

Update

by looking at your index.js file it looks like you didn't add the facebook oauth to your 3-party authentication logins. so you will need to add the following:

oauth: {
   facebook: {
     appIds: "FACEBOOK APP ID"
   }
}

below your emailAdapter config and inside "FACEBOOK APP ID" put the app ID that you created in facebook developers

Sign up to request clarification or add additional context in comments.

16 Comments

That code didn't work for me either. I still get the same error in the logs as I posted in the question about "Cant set headers." Could you please tell me what version of parse you're using? If you have any settings in heroku that you had to have to make it work? I am using the most updated version of parse and I was using OneSignal Push Adapter as well in my index.js file. But I was getting that error way before I used the push adapter.
I am using the latest version of parse 2.2.17. Can you please try to call to response.success() in the success block and response.error() in the error block and see if the callback is being called?
Okay, I am using that version as well. Do you have any other suggestions on maybe what the problem is? Or what I can do to fix it? Before I updated Parse, I was getting an error on my .p12 file saying it was unrecognized. Even though it was there. Then I updated parse on my terminal, and that error went away. But now ever since I have been getting the error about not being able to set headers after their sent.
I just used your method to implement the "response" methods in the code blocks, and still got the same error. I feel like I literally have tried everything, and nothing has worked lol. Ive been stuck on this 1 error for 3 days.
When I send pushes from the OneSignal dashboard, they go to my phone. Its just when I try and call the cloud code from my app is when I have the problem. I could also have sent pushes from my Parse dashboard as well when I was using Parse for the Push Notifications. But again, it didn't work from the Cloud Code.
|
0

in main.js put this code

// SEND PUSH NOTIFICATION
Parse.Cloud.define("push", function(request, response) {

  var user = request.user;
  var params = request.params;
  var someKey = params.someKey
  var data = params.data

  var recipientUser = new Parse.User();
  recipientUser.id = someKey;

  var pushQuery = new Parse.Query(Parse.Installation);
  pushQuery.equalTo("userID", someKey);


  Parse.Push.send({
    where: pushQuery, // Set our Installation query
    data: data
  }, { success: function() {
      console.log("#### PUSH OK");
  }, error: function(error) {
      console.log("#### PUSH ERROR" + error.message);
  }, useMasterKey: true});

  response.success('success');
});



// SEND PUSH NOTIFICATION FOR ANDROID
Parse.Cloud.define("pushAndroid", function(request, response) {

  var user = request.user;
  var params = request.params;
  var someKey = params.someKey
  var data = params.data

  var recipientUser = new Parse.User();
  recipientUser.id = someKey;

  var pushQuery = new Parse.Query(Parse.Installation);
  pushQuery.equalTo("userID", someKey);


  Parse.Push.send({
    where: pushQuery, // Set our Installation query
    data: {
       alert: data
    }
}, { success: function() {
      console.log("#### PUSH OK");
  }, error: function(error) {
      console.log("#### PUSH ERROR" + error.message);
  }, useMasterKey: true});

  response.success('success');
});

in xcode project do like that to send push notification

     // Send Push notification
        let pushStr = "@\(PFUser.current()![USER_USERNAME]!) | \n\(self.lastMessageStr)"


        let data = [ "badge" : "Increment",
                     "alert" : pushStr,
                     "sound" : "bingbong.aiff",

            ] as [String : Any]
        let request = [
            "someKey" : self.userObj.objectId!,
            "data" : data

            ] as [String : Any]
        PFCloud.callFunction(inBackground: "push", withParameters: request as [String : Any], block: { (results, error) in
            if error == nil {
                print ("\nPUSH SENT TO: \(self.userObj[USER_USERNAME]!)\nMESSAGE: \(pushStr)\n")
            } else {
                print ("\(error!.localizedDescription)")
            }
        })

1 Comment

Please explain some of the code you have written. You help is appreciated, but if OP knows details about your code, it would make more sense

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.