Search code examples
iospush-notificationapple-push-notificationsparse-serverheroku-api

Push notifications, from Parse-Server cloud code


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?


Solution

  • 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
      });
    });