1

I'm trying to set up push notifications from one user to another using Back4App which is a parse server. I have followed their guide here

the Javascript cloud code they use is below:

Parse.Cloud.define("pushsample", function (request, response) {
Parse.Push.send({
        channels: ["News"],
        data: {
            title: "Hello from the Cloud Code",
            alert: "Back4App rocks!",
        }
   }, {
        success: function () {
            // Push was successful
            response.success("push sent");
            console.log("Success: push sent");
        },
        error: function (error) {
            // Push was unsucessful
            response.error("error with push: " + error);
            console.log("Error: " + error);
        },
        useMasterKey: true
   });
});

I am updating a custom parse class called notifications within the app which I would also like to send to the user the notification is directed at. When saving this class I am grabbing the UserID which is also stored in the installation class used to send pushes. I am completely new to Javascript so am wondering if someone could tell me how to edit the above code to receive the userID from the method on the device and then run a query to send to just this user.

Tom Fox
  • 897
  • 3
  • 14
  • 34
Khledon
  • 193
  • 5
  • 23
  • Hi! (Just checking) Have you enabled the "Push Notification from Client" available at Server Settings > Core Settings > Settings? – Charles Apr 25 '19 at 00:13
  • Yeah all set on that side of things! Just not sure how to do what I need in JavaScript! – Khledon Apr 25 '19 at 06:58

1 Answers1

3

The push notification feature allows to configure options and customizing the push.

You can send a query to update one specific user. Please, take a look at the example below:

Parse.Cloud.define("sendPushToUser", 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: "Ricky Vaughn was injured in last night's game!",
    name: "Vaughn"
  }
})
.then(function() {
  // Push was successful
}, function(error) {
  // Handle error
});

});

At the moment, you can read more about these options here.

nataliec
  • 502
  • 4
  • 14
  • This makes sense for the cloud code, however I'm still unsure how this would be called in swift. How would I send the UserId to be used in the code above? – Khledon Apr 25 '19 at 12:16
  • 3
    @Khledon I found the article below that might help you with it (Step #5): https://www.back4app.com/docs/ios/push-notifications/send-push-using-cloud-code-swift – Charles Apr 25 '19 at 16:11