Deema Al-Shami
Deema Al-Shami

Reputation: 31

Send parse push notification on button click android

What I need is when the user clicks on a button, a push notification will be sent to all users using this app. I tried using this code in a setOnclickListener method, but nothing was sent after clicking on it.

Note: Sending a push notification from Parse Dashboard works perfectly fine.

        ParsePush parsePush = new ParsePush();
        ParseQuery query = ParseInstallation.getQuery();
        parsePush.setQuery(query);
        parsePush.setMessage("A new file has been Updated. Check it out!");
        parsePush.sendInBackground(new SendCallback() {

            @Override
            public void done(ParseException arg0) {
                // TODO Auto-generated method stub
                Log.d(TAG, "SendCallback success:");
                if(arg0 == null)
                {
                    Log.d(TAG,
                            "suceess push notification :");
                }
                else
                {
                    Log.d(TAG,
                            "failed push notification  :"
                                    + arg0.getMessage());
                }
            }
        });
    }
});

- EDIT - in response to Bradley Wilson's answer - this still did not work

ParsePush parsePush = new ParsePush();
ParseQuery<ParseInstallation> parseQueryInstallation = ParseQuery.getQuery(ParseInstallation.class);
parsePush.setQuery(parseQueryInstallation);
parsePush.setMessage("A new file has been Updated. Check it out!");
parsePush.sendInBackground(new SendCallback() {

    @Override
    public void done(ParseException arg0) {
        // TODO Auto-generated method stub
        Log.d(TAG, "SendCallback success:");
        if(arg0 == null)
        {
            Log.d(TAG,
                    "suceess push notification :");
        }
        else
        {
            Log.d(TAG,
                    "failed push notification  :"
                            + arg0.getMessage());
        }
    }

- EDIT 2 - in response to Suresh Kumar's answer - For some reason, cloud code never works in my projects. It just doesn't identify any Cloud code and keeps it in red as shown in this image

Upvotes: 1

Views: 125

Answers (1)

Suresh Kumar
Suresh Kumar

Reputation: 2034

The better way to send push notification in Parse is through cloud code. Create a cloud function to send push notification and call that cloud function in android using ParseCloud.callFunctionInBackground().

HashMap<String, Object> params = new HashMap<String, Object>();
params.put("recipientId", userObject.getObjectId());
params.put("message", message);
ParseCloud.callFunctionInBackground("sendPushToUser", params, new FunctionCallback<String>() {
   void done(String success, ParseException e) {
       if (e == null) {
          // Push sent successfully
       }
   }
});

Take a look at this link

Upvotes: 1

Related Questions