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 Answers2

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

Ran Hassid
  • 2,788
  • 1
  • 12
  • 20
  • 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. – Brennan Hamstra Jul 31 '16 at 18:40
  • 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? – Ran Hassid Jul 31 '16 at 18:42
  • 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. – Brennan Hamstra Jul 31 '16 at 18:47
  • 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. – Brennan Hamstra Jul 31 '16 at 18:56
  • 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. – Brennan Hamstra Jul 31 '16 at 19:05
  • I have found out it is a Node.js error. I cant use my cloud code for anything. Even if I only do "Console.log("This is a test")" , it still throws the same error. – Brennan Hamstra Aug 01 '16 at 01:54
  • Can you please paste your index.js file (i want to see how you configure the cloud code file) – Ran Hassid Aug 01 '16 at 05:51
  • I just added the file as a picture. – Brennan Hamstra Aug 01 '16 at 07:11
  • ok, i edit my answer. You should add facebook to your 3-party login providers.. look at the answer for further instructions.. – Ran Hassid Aug 01 '16 at 07:27
  • Okay, I will do that. Is this just an implementation I should do. Or will this possibly fix my issue? – Brennan Hamstra Aug 01 '16 at 07:53
  • Okay, I just implemented the oauth function. Still got the same error though. Not sure if that was supposed to solve it or not. But thank you for recommending that. I also appreciate all of your help so far too. – Brennan Hamstra Aug 01 '16 at 08:01
  • Do you know how to roll back Parse Server Versions? The error is caused by a 2.2.17 bug. So I need 2.2.16. Do you know how to do that? – Brennan Hamstra Aug 01 '16 at 21:36
  • try npm install parse-server@2.2.16 – Ran Hassid Aug 02 '16 at 03:29
  • It didn't work. After it was done downloading. it said "parse-server@2.2.16 - INVALID" – Brennan Hamstra Aug 02 '16 at 05:01
  • for me it worked.. you need to run this command inside the folder where you install parse-server. If you want to modify the deployed version then you need to change the package.json file to point to 2.2.16 – Ran Hassid Aug 02 '16 at 05:07
  • I got it working earlier today, thank you very much for your help. I appreciate it greatly. – Brennan Hamstra Aug 03 '16 at 06:32
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)")
            }
        })
Faris
  • 1,206
  • 1
  • 12
  • 18
  • 1
    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 – Akshansh Thakur Jan 19 '18 at 05:55