0

I am trying to make Push notifications work, on Parse-Server (Heroku), with an iOS app.

At this point I can receive a notification in my iOS app, by running the command:

curl -X POST \
  -H "X-Parse-Application-Id: 12345678ABCDEFstuvwxyz" \
  -H "X-Parse-Master-Key: ABCDEF12345678stuvwxyz" \
  -H "Content-Type: application/json" \
  -d '{
        "where": {
          "deviceType": {
            "$in": ["ios"]
          }
        },
        "data": {
            "aps": {
              "alert": {},
              "content-available": 1
            },
            "title": "The Shining",
            "alert": {},
            "content-available": 1
        }
      }'\   https://myapp.herokuapp.com/parse/push

But now I must send a push notification from cloud code, namely from a Parse.Cloud.afterSave function.

This is what I have tried, following some sample code I found on the web, but it is not working :

  Parse.Cloud.afterSave("Item_List", (request) => {
    Parse.Push.send({
      ??????
      data: {"alert": "NOTIFICATION-FOR-USERS"},
      }, { success: function() {
         console.log("#### PUSH OK");
      }, error: function(error) {
         console.log("#### PUSH ERROR" + error.message);
      }, useMasterKey: true});
  });

What is the proper way to get what I want?

Michel
  • 10,303
  • 17
  • 82
  • 179

1 Answers1

0

Try something like this:

Parse.Cloud.afterSave('Item_List', async () => {
  const query = new Parse.Query(Parse.Installation);
  query.equalTo('deviceType','ios');
  await Parse.Push.send({
    where: query,
    data: { alert: 'NOTIFICATION-FOR-USERS' },
    useMasterKey: true
  });
});
Davi Macêdo
  • 2,954
  • 1
  • 8
  • 11
  • In fact I made some progress and am using similar code, but without the async and the await. Should I use it? – Michel Mar 02 '20 at 08:22
  • This is now my current issue: https://stackoverflow.com/questions/60484864/how-can-i-customize-a-push-notification – Michel Mar 02 '20 at 08:23
  • I prefer the async/await style since it is less verbose. – Davi Macêdo Mar 03 '20 at 00:04
  • I see, I will try both. Is there any fundamental difference between the two other than the style preferrence? – Michel Mar 03 '20 at 01:34
  • It is only the stile preference, but, in the case that you want to return to the client only after the afterSave trigger has finished, you have to make sure that your function returns a promise or you can use the async/await format (which makes your function to return a promise by default). – Davi Macêdo Mar 04 '20 at 01:08