java - Message failed to send -
java - Message failed to send -
i using sinch parse, , on messagingactivity receiving next message: message failed send (in method onmessagefailed). trying understand why message failed register parse:, , sent together
below activity believe triggering error:
public class messagingactivity1 extends activity { private string recipientid; private edittext messagebodyfield; private string messagebody; private messageservice.messageserviceinterface messageservice; private messageadapter messageadapter; private listview messageslist; private string currentuserid; private serviceconnection serviceconnection = new myserviceconnection(); private messageclientlistener messageclientlistener = new mymessageclientlistener(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.messaging); parse.initialize(this, "code", "code"); bindservice(new intent(this, messageservice.class), serviceconnection, bind_auto_create); intent intent = getintent(); recipientid = intent.getstringextra("recipient_id"); currentuserid = parseuser.getcurrentuser().getobjectid(); messageslist = (listview) findviewbyid(r.id.listmessages); messageadapter = new messageadapter(this); messageslist.setadapter(messageadapter); populatemessagehistory(); messagebodyfield = (edittext) findviewbyid(r.id.messagebodyfield); findviewbyid(r.id.sendbutton).setonclicklistener( new view.onclicklistener() { @override public void onclick(view view) { sendmessage(); } }); } // previous messages parse & display private void populatemessagehistory() { string[] userids = { currentuserid, recipientid }; parsequery<parseobject> query = parsequery.getquery("user"); query.wherecontainedin("senderid", arrays.aslist(userids)); query.wherecontainedin("recipientid", arrays.aslist(userids)); query.orderbyascending("createdat"); query.findinbackground(new findcallback<parseobject>() { @override public void done(list<parseobject> messagelist, com.parse.parseexception e) { if (e == null) { (int = 0; < messagelist.size(); i++) { writablemessage message = new writablemessage( messagelist.get(i).get("recipientid") .tostring(), messagelist.get(i) .get("messagetext").tostring()); if (messagelist.get(i).get("senderid").tostring() .equals(currentuserid)) { messageadapter.addmessage(message, messageadapter.direction_outgoing); } else { messageadapter.addmessage(message, messageadapter.direction_incoming); } } } } }); } private void sendmessage() { messagebody = messagebodyfield.gettext().tostring(); if (messagebody.isempty()) { toast.maketext(this, "please come in message", toast.length_long) .show(); return; } messageservice.sendmessage(recipientid, messagebody); messagebodyfield.settext(""); } @override public void ondestroy() { messageservice.removemessageclientlistener(messageclientlistener); unbindservice(serviceconnection); super.ondestroy(); } private class myserviceconnection implements serviceconnection { @override public void onserviceconnected(componentname componentname, ibinder ibinder) { messageservice = (messageservice.messageserviceinterface) ibinder; messageservice.addmessageclientlistener(messageclientlistener); } @override public void onservicedisconnected(componentname componentname) { messageservice = null; } } private class mymessageclientlistener implements messageclientlistener { @override public void onmessagefailed(messageclient client, message message, messagefailureinfo failureinfo) { toast.maketext(messagingactivity1.this, "message failed send.", toast.length_long).show(); } @override public void onincomingmessage(messageclient client, message message) { if (message.getsenderid().equals(recipientid)) { writablemessage writablemessage = new writablemessage(message .getrecipientids().get(0), message.gettextbody()); messageadapter.addmessage(writablemessage, messageadapter.direction_incoming); } } @override public void onmessagesent(messageclient client, message message, string recipientid) { final writablemessage writablemessage = new writablemessage(message .getrecipientids().get(0), message.gettextbody()); // add together message parse database if doesn't exist // there parsequery<parseobject> query = parsequery.getquery("user"); query.whereequalto("doobaid", message.getmessageid()); query.findinbackground(new findcallback<parseobject>() { @override public void done(list<parseobject> messagelist, com.parse.parseexception e) { if (e == null) { if (messagelist.size() == 0) { parseobject parsemessage = new parseobject( "user"); parsemessage.put("senderid", currentuserid); parsemessage.put("recipientid", writablemessage .getrecipientids().get(0)); parsemessage.put("messagetext", writablemessage.gettextbody()); parsemessage.put("doobaid", writablemessage.getmessageid()); parsemessage.saveinbackground(); messageadapter.addmessage(writablemessage, messageadapter.direction_outgoing); } } } }); } @override public void onmessagedelivered(messageclient client, messagedeliveryinfo deliveryinfo) { } @override public void onshouldsendpushdata(messageclient client, message message, list<pushpair> pushpairs) { } } }
below messageservice activity:
public class messageservice extends service implements sinchclientlistener { private static final string app_key = "code; private static final string app_secret = "code"; private static final string environment = "code"; private final messageserviceinterface serviceinterface = new messageserviceinterface(); private sinchclient sinchclient = null; private messageclient messageclient = null; private string currentuserid; private localbroadcastmanager broadcaster; private intent broadcastintent = new intent( "com.dooba.beta.matchoptionactivity1"); @override public int onstartcommand(intent intent, int flags, int startid) { currentuserid = parseuser.getcurrentuser().getobjectid(); if (currentuserid != null && !issinchclientstarted()) { startsinchclient(currentuserid); } broadcaster = localbroadcastmanager.getinstance(this); homecoming super.onstartcommand(intent, flags, startid); } public void startsinchclient(string username) { sinchclient = sinch.getsinchclientbuilder().context(this) .userid(username).applicationkey(app_key) .applicationsecret(app_secret).environmenthost(environment) .build(); sinchclient.addsinchclientlistener(this); sinchclient.setsupportmessaging(true); sinchclient.setsupportactiveconnectioninbackground(true); sinchclient.checkmanifest(); sinchclient.start(); } private boolean issinchclientstarted() { homecoming sinchclient != null && sinchclient.isstarted(); } @override public void onclientfailed(sinchclient client, sincherror error) { broadcastintent.putextra("success", false); broadcaster.sendbroadcast(broadcastintent); sinchclient = null; } @override public void onclientstarted(sinchclient client) { broadcastintent.putextra("success", true); broadcaster.sendbroadcast(broadcastintent); client.startlisteningonactiveconnection(); messageclient = client.getmessageclient(); } @override public void onclientstopped(sinchclient client) { sinchclient = null; } @override public ibinder onbind(intent intent) { homecoming serviceinterface; } @override public void onlogmessage(int level, string area, string message) { } @override public void onregistrationcredentialsrequired(sinchclient client, clientregistration clientregistration) { } public void sendmessage(string recipientuserid, string textbody) { if (messageclient != null) { writablemessage message = new writablemessage(recipientuserid, textbody); messageclient.send(message); } } public void addmessageclientlistener(messageclientlistener listener) { if (messageclient != null) { messageclient.addmessageclientlistener(listener); } } public void removemessageclientlistener(messageclientlistener listener) { if (messageclient != null) { messageclient.removemessageclientlistener(listener); } } @override public void ondestroy() { sinchclient.stoplisteningonactiveconnection(); sinchclient.terminate(); } public class messageserviceinterface extends binder { public void sendmessage(string recipientuserid, string textbody) { messageservice.this.sendmessage(recipientuserid, textbody); } public void addmessageclientlistener(messageclientlistener listener) { messageservice.this.addmessageclientlistener(listener); } public void removemessageclientlistener(messageclientlistener listener) { messageservice.this.removemessageclientlistener(listener); } public boolean issinchclientstarted() { homecoming messageservice.this.issinchclientstarted(); } } }
the variables
private serviceconnection serviceconnection = new myserviceconnection(); private messageclientlistener messageclientlistener = new mymessageclientlistener();
may need parse
initialized much before oncreate
, phone call parse.initialize
;
try moving parse.initialize
application
class.
java android android-layout android-activity parse.com
Comments
Post a Comment