我正在尝试做ChatApp。我可以互相和别人说话。但是有一个问题。我希望在向任何用户发送消息时使用推送通知。 所以我正在添加这些信息。我在Parse上有这些课程。 Intallation,Session,User,Chat。 我正在为用户使用User类,为每个对话使用Chat类,例如有人发送另一个消息,我正在添加一个对象我的聊天类。
所以我不知道如何发送推送通知。我想我应该使用User objectId来寻找好友。在这里我要添加我的sendMessage方法:
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();
}
});
}
答案 0 :(得分:0)
使用以下代码发送推送通知:
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();
}
buddyObjectId是伙伴对象id
chatObjectID是保存后的聊天消息对象ID
type是消息的类型(如果有)。
还要确保在创建用户时将用户对象ID保存在安装表的owner列中。