Francesca Casoli
Francesca Casoli

Reputation: 1

Can't send push notifications via REST api when migrating from Parse to Nodechef

we are migrating our apps to Nodechef parse server with DBs hosted on Object Rocket. While we are able to send pushes via Nodechef dashboard, we are unable to send pushes via REST api as we currently do with Parse.

This is the request sent from Nodechef dashboard:

POST /parse/push HTTP/1.1 
Host: test-appname-1067.nodechef.com 
Connection: keep-alive 
Content-Length: 446 
Origin: https://dashboard.nodechef.com 
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N)     AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile    Safari/537.36 
Content-Type: text/plain 
Accept: / 
Referer: https://dashboard.nodechef.com/apps/test-appname/push/new 
Accept-Encoding: gzip, deflate, br 
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4 
{"where": {"deviceType":{"$in":["ios"]}},"data":{"alert":"Text of the alert","category":"category of the alert","title":"Title of the alert","u":"2b30Yuh"},"_ApplicationId":"applicationid","_ClientVersion":"js1.6.14","_MasterKey":"masterkey","_InstallationId":"56205783-4f18-7427-919a-6e03ed80443a"}

This is the request we are trying to send (same structure used with Parse):

curl -X POST \
-H "X-Parse-Application-Id: applicationid" \
-H "X-Parse-Master-Key: masterkey" \
-H "Content-Type: application/json" \
-d 
{"where": {"deviceType":{"$in":["ios"]}},"data": "{\"alert\":\"Text of the alert\", \"category\": \"category of the alert\", \"title\": \"Title of the alert\", \"u\": \"2b30Yuh\"}"
} \
https://test-appname-1067.nodechef.com/parse/push

We get no error (result:true) when trying this curl but no push is sent at all.

We noticed several differences between the two requests so we tried to reproduce the same request sent via dashboard and still we were not able to send anything:

we changed the content type in the header we specified the various keys in the body instead of the header we specified that "installation id" we have no clue of what is it

The only other difference we saw is that Nodechef dashboard makes use of Parse SDK for javascript, while we are using the php SDK, but honestly we don't think this could be the reason why requests from REST api fail

Already opened an issue to Nodechef, they couldn't help and suggested to post on the parse server repo on github for further assistance, where they asked to open a issue here.

Thank you in advance for any help

Upvotes: 0

Views: 386

Answers (1)

Ran Hassid
Ran Hassid

Reputation: 2788

currently sending push notifications can be done only with master key. So the only way to do it is from cloud code. In order to send a push you need to do the following steps:

  1. Create cloud code function (let's call it sendPush for instance)
  2. The cloud code function can receive parameters so you can send any parameters that you like in order to know what exactly needs to be done in cloud code before you sending the push. For example: to which users you like to send, or to which channel etc.
  3. You need to add useMasterKey to where you send the push so at the end your code should look like this:

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

  // You can get parameters in here... You can access to specific parameter like this: 
  // request.params.{PARAM_NAME}

  // build the query for the push notification 
  // the query can be built by your parameters (e.g. to which userId or channel id etc.)
  var query = new Parse.Query(Parse.Installation);
  query.exists("deviceToken");

  // this is the push payload 
  var payload = {
    alert: "after save push",
    sound: "default"
      // ... add more here if required 
  };

  Parse.Push.send({
      data: payload,
      where: query
    }, {
      useMasterKey: true
    }) // useMasterKey is required currently 
    .then(function() {
      response.success("Push Sent!");
    }, function(error) {
      response.error("Error while trying to send push " + error.message);
    });
});

More info about cloud code and how to use it can be found here

Upvotes: 1

Related Questions