Sending push notification using Parse SDK on Android

225 views Asked by At

I am trying to do ChatApp. I can do talking people each other. But there is an issue. I want to use push notification when a message sended to any user. So i am adding these information. I have these classses on Parse. Intallation , Session , User, Chat. I am using User class for users , Chat class for each conversation for example someone senf a message another one , i am adding an object my chat class.

So I dont know how can i send push notification. I guess i should use User objectId to finding buddy. And here i am adding my sendMessage method:

private void sendMessage() {
    if (txt.length()==0)
       return;
    InputMethodManager imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(txt.getWindowToken(), 0);

    String s=txt.getText().toString();
    final Conversation c=new Conversation(s,new Date(),UserList.user.getUsername());
    c.setStatus(Conversation.STATUS_SENDING);
    convList.add(c);
    adp.notifyDataSetChanged();
    txt.setText(null);

    ParseObject po=new ParseObject("Chat");
    po.put("sender",UserList.user.getUsername());
    po.put("receiver",buddy);
    po.put("message",s);
    po.saveEventually(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null){
                c.setStatus(Conversation.STATUS_SENT);
            }
            else {
                c.setStatus(Conversation.STATUS_FAILED);
            }
            adp.notifyDataSetChanged();
        }
    });
}
1

There are 1 answers

3
hemanth1488 On

Use the below code to send the push notification:

public void  sendpushnotification(String buddyObjectId,String message,String type,String chatObjectID){
   ParseQuery query = ParseInstallation.getQuery(); 

    query.whereEqualTo("owner",owner);    
    ParsePush push = new ParsePush();
    push.setQuery(query);
    try {
        JSONObject data = new JSONObject("{\"action\": \"{Action name}\",\"alert\":\""+message+"\",\"mid\":\""+chatObjectID+"\",\"pid\":\""+chatObjectID+"\",\"t\": \""+type+"\"}");
        push.setData(data);
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    push.sendInBackground();

 }

Where buddyObjectId is buddy object id

chatObjectID is chat message object id after saving

type is type of message if any.

Also make sure to save user object id in owner column of the installation table when a user is created.