0

I'm trying to send notification to an specefic device using Parse In android. this is my code for ParseInstallation:

 ParseInstallation installation = ParseInstallation.getCurrentInstallation();
    installation.put("device_id", "1234567890");
    installation.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            Log.d(TAG, "done1: "+e);
        }
    });

and this is my code for sending the notificaiton to specefic device that i already installed :

ParseQuery query = ParseInstallation.getQuery();
query.whereEqualTo("device_id", "1234567890");
ParsePush push = new ParsePush();
push.setQuery(query);
push.setMessage("salamm");
push.sendInBackground(new SendCallback() {
    @Override
    public void done(ParseException e) {
        Log.d(TAG, "done: "+e);
    }
});

and i get this error in log : done: com.parse.ParseRequest$ParseRequestException: unauthorized: master key is required

can anyone help me with this?

2 Answers2

1

For security reasons, it is not recommended to send pushes directly from the frontend. Imagine that a hacker could then send a bad massage to all your customer base.

The recommended way to do this: - Create a cloud code function to send the push - Android app will call this cloud code function

This is how your cloud code function should like:

Parse.Cloud.define('sendPush', function(request, response) {
  const query = new Parse.Query(Parse.Installation);
  query.equalTo('device_id', request.params.deviceId);
  Parse.Push.send({
    where: query,
    data: {
      alert: request.params.message
    }
  },
  { useMasterKey: true }
  )
  .then(function() {
    response.success();
  }, function(error) {
    response.error(error);
  });
});

And this is how your client code should like:

HashMap<String, String> params = new HashMap();
params.put("deviceId", "1234567890");
params.put("message", "salamm");
ParseCloud.callFunctionInBackground("sendPush", params, new
FunctionCallback<Object>() {
  @Override
  public void done(Object result, ParseException e) {
    Log.d(TAG, "done: "+e);
  }
});
Davi Macêdo
  • 2,954
  • 1
  • 8
  • 11
0

Parse Server mo longer supports client push as it is a significant security risk. The best alternative is to put this logic in a cloud code function and call that via the Android SDK.

See the section on sending push notification in the JS guide for more information.

Remember to add use {useMasterKey:true}

Tom Fox
  • 897
  • 3
  • 14
  • 34