1

I'm trying to call a cloud code function in Parse but whenever it gets called I get the following error, but have no idea why it is invalid:

2019-04-30T11:0 1:44.020Z - Invalid function: "pushTenFTC"

Cloud Code:

Parse.Cloud.define("pushTenFTC", async (request) => {
var query = new Parse.Query(Parse.Installation);
let userId = request.params.userId;
query.equalTo('userId', userId);

Parse.Push.send({
    where: query,
    data: {
        alert: "Fitcoins Gifted!",
        title: userId + " sent you 10 Fitcoins!"
    }
}).then(function() {
    // Push was successful
}, function(error) {
    // Handle error
});

Called in Swift:

var params = [AnyHashable: Any]()
params["userId"] = feedElements[sender.tag].objectID
PFCloud.callFunction(inBackground: "pushTenFTC", withParameters: params) { (response, error) in
    if let error = error {
        //error handling
        return
    }
    //Success
}
Tom Fox
  • 897
  • 3
  • 14
  • 34
Khledon
  • 193
  • 5
  • 23

1 Answers1

2

It seems you are missing }); at the end of the function and your masterKey is also required to send push notifications.

The whole function should look like this...

Parse.Cloud.define("pushTenFTC", async (request) => {
  var query = new Parse.Query(Parse.Installation);
  let userId = request.params.userId;
  query.equalTo('userId', userId);

  Parse.Push.send({
      where: query,
      data: {
          alert: "Fitcoins Gifted!",
          title: userId + " sent you 10 Fitcoins!"
      }
  }, {useMasterKey: true}).then(function() {
      // Push was successful
  }, function(error) {
      // Handle error
  });
});
Tom Fox
  • 897
  • 3
  • 14
  • 34
  • Thats seems to have fixed it, but I now get the following error: status: 403, message: 'unauthorized: master key is required' – Khledon Apr 30 '19 at 16:02
  • Does the master key need to be configured anywhere first? I'm getting the same error, but from further research it appears as though the code you have posted above should work. Any other ideas? – Khledon Apr 30 '19 at 19:52
  • I could be wrong but, just thinking I put the `masterKey` in the wrong place - see my new edit. – Tom Fox Apr 30 '19 at 21:31
  • Thanks! I think that worked! But now no pushes are sent through to and I get this error: 'stream ended unexpectedly', when I try to send a push to all users. This was working fine on Xcode before I published to testflight, so I'm wondering if its related to my certificates (they look ok though). I'm new to iOS development... and this is by far the hardest thing I've tried to get ups and running! – Khledon May 01 '19 at 09:28