Brennan Hamstra
Brennan Hamstra

Reputation: 93

Parse Push Notification Cloud Code Error?

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 `

Upvotes: 1

Views: 397

Answers (2)

Faris
Faris

Reputation: 1236

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)")
            }
        })

Upvotes: 0

Ran Hassid
Ran Hassid

Reputation: 2788

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

Upvotes: 1

Related Questions