android - How to add custom extension(time) to <message> tag? -
i'm trying add time extension message custom packet extension. seems time gets added while sending message. @ receiver end, shows null.
this packet extension class , embeddedextensionprovider
public class timestampextension implements packetextension { public static final string element = "timestamp"; public static final string namespace = "urn:xmpp:timestamp"; public string time = null; public timestampextension() { } public void settime() { simpledateformat time = new simpledateformat("hh:mm a"); date datetime = new date(); string strtime = time.format(datetime); this.time = strtime; } public string gettime() { return time; } @override public string getelementname() { return element; } @override public string getnamespace() { return namespace; } @override public string toxml() { return "<" + element + " xmlns='" + namespace + "'><time>" + time + "</time></" + element + ">"; } public static class provider extends embeddedextensionprovider { @override protected packetextension createreturnextension(string arg0, string arg1, map<string, string> arg2, list<? extends packetextension> arg3) { return new timestampextension(); } } }
registering provider
providermanager.getinstance().addextensionprovider( timestampextension.element, timestampextension.namespace, new timestampextension.provider());
sending message extension
timestampextension timeext = new timestampextension(); timeext.settime(); msg.addextension(timeext); connection.sendpacket(msg);
message xml output @ sender:
<message id="m55k5-7" to="testfirzan@sushant" type="chat"> <body>hi</body><request xmlns='urn:xmpp:receipts'/> <timestamp xmlns='urn:xmpp:timestamp'> <time>01:04 pm</time> </timestamp>
message xml output @ receiver:
<message id="m55k5-7" to="testfirzan@sushant" from="testsushant@sushant/smack" type="chat"> <body>hi</body><request xmlns='urn:xmpp:receipts'/> <timestamp xmlns='urn:xmpp:timestamp'> <time>null</time> </timestamp>
it's supposed show sending time in time element. i'm doing wrong ?
sorry folks late answer.
since time mandatory parameter me, end using defaultpacketextension
//to sent message private void sendmessage(string message, string recipient) { message msg = new message(recipient, message.type.chat); msg.setbody(message); //getting current timestamp in string format string messagetimestamp = string.valueof(system.currenttimemillis()); //creating default packet extension name 'timestamp' , urn 'urn:xmpp:timestamp' defaultpacketextension exttimestamp = new defaultpacketextension( "timestamp", "urn:xmpp:timestamp"); //setting value in extension exttimestamp.setvalue("timestamp", messagetimestamp); //add extension message tag msg.addextension(exttimestamp); //send message xmppconnection.sendpacket(message); } //to receive , parse message extension implement packetlistener @override public void processpacket(packet packet) { message message = (message) packet; //get extension message defaultpacketextension exttimestamp = (defaultpacketextension) message .getextension("urn:xmpp:timestamp"); //get value extension long timestamp = long.parselong(exttimestamp.getvalue("timestamp")); system.out.println("message :" + message.getbody() + " timestamp: "+timestamp); }
Comments
Post a Comment