Parse Target Push Notification Specific User

2019-05-18 22:51发布

问题:

So let's say I have two users of my app which has a messaging component. Using parse, is there a way I can send a push notification from one user to the other?

EDIT: assume I have a way to uniquely identify users, for example email address

回答1:

Yes its possible.

  1. First enable client push in your parse admin console

  2. Using ParsePush class, we have an option to set the ParseInstallation Query, where the notifications would be targeted to specific set of users,

https://parse.com/docs/push_guide#options-data/iOS

But you may NOT be able to send to a specific user, since the ParseInstallation table doesnt have such column to have it in query constraint.

So I added a new field 'username' in ParseInstallation table, and update the username whenever the user log in.

Then while sending the push, create the ParseInstallation query based on this username, the below example is for android, and it would be similar for other OS

 JSONObject data = new JSONObject();
    try {
        data.put("action", "com.bt.yana.GOT_MESSAGE");
        data.put("from", ParseUser.getCurrentUser().getUsername());
    }catch (Exception e){
        e.printStackTrace();
        return;
    }


    ParsePush parsePush = new ParsePush();
    parsePush.setData(data);

    ParseQuery<ParseInstallation> parseQuery = ParseQuery.getQuery(ParseInstallation.class);
    if(toUser!=null) {
        parseQuery.whereEqualTo("username", toUser);
        parsePush.setQuery(parseQuery);
        parsePush.sendInBackground();
    }