Parse Push Notification Cloud Code Error?

こ雲淡風輕ζ 提交于 2019-12-11 05:03:22

问题


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:

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: `


回答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




回答2:


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


来源:https://stackoverflow.com/questions/38679452/parse-push-notification-cloud-code-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!