I use asmack building a jabber client for android, it works well ,but I got a problem when sending a large text in Chinese. The XMPPConnection just disconnected from server immediately,here are my Codes of Connection initialization:
ConnectionConfiguration c = new ConnectionConfiguration(mServerAddress, mPort);
c.setSecurityMode(SecurityMode.disabled);
c.setReconnectionAllowed(true);
c.setCompressionEnabled(false);
mConn = new XMPPConnection(c);
and I send a chat message like this:
final Message msg = new Message(chatMsg.getTo(), Message.Type.chat);
msg.setBody(chatMsg.getBody());
new Thread() {
#Override
public void run() {
if(mConn != null && mConn.getUser() != null) {
mConn.sendPacket(msg);
}
}
}.start();
Related
I enabled mam archiving in ejabberd , when I send a messsage to users ,
the receiver of message has information about timestamp of message in received packet like bellow
<archived by='989138553343#192.168.43.67' id='1508653008093085'
xmlns='urn:xmpp:mam:tmp'/>
is it pussible to get this info in StanzaIdAcknowledgedListener that I am using it like bellow :
(I am using ejabbeejabberd-17.08 server and smack in android client .)
mConnection.addStanzaIdAcknowledgedListener(stanzaId, new
StanzaListener() {
#Override
public void processStanza(final Stanza packet) throws
SmackException.NotConnectedException, InterruptedException {
AndroidUtilities.runOnUIThread(new Runnable() {
#Override
public void run() {
//handle ack received by server
}
});
}
});
Yes, it is possible using DelayInformation message extension if you have got it in message stanza.use below namespace and elementname for get id and timestamp.
Message msg = (Message) stanza; //your stanza packet
if(msg.getExtension(DeliveryReceipt.ELEMENT,DeliveryReceipt.NAMESPACE) !=null){
DelayInformation timestamp = (DelayInformation)msg.getExtension("delay", "urn:xmpp:delay");
if (timestamp == null)
timestamp = (DelayInformation)msg.getExtension("x", "jabber:x:delay");
if (timestamp != null)
ts = timestamp.getStamp().getTime();
else
ts = System.currentTimeMillis();
}
I am developing a chat application. In that if i am online, sending and receiving messages with current date and time is fine. But if i am offline again coming to online after 2 hours, i am receiving messages with current time itself, but i want to receive the message time with sender time. help me out of this.
Thanks in advance.
send part
if (connection != null) {
connection.sendPacket(msg);
data = new Msg();
data.setMessage(text);
data.setSenderName(userFrom);
String date = DateAndTime.getCurrentDate();
data.setDate(date);
String time = DateAndTime.getCurrentTime();
data.setTime(time);
data.setSender(true);
MessageListAdapter.messagesItems.add(data);
notifyMyAdapter();
}
receive part
if (connection != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
#Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
String from = to + "#localhost/Smack";
if (from.equalsIgnoreCase(message.getFrom())) {
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Msg data = new Msg();
String subject = message.getSubject();
if (subject == null) {
String text = message.getBody();
data.setMessage(text);
data.setSenderName(NAME);
String date = DateAndTime.getCurrentDate();
data.setDate(date);
String time = DateAndTime.getCurrentTime();
data.setTime(time);
data.setSender(false);
MessageListAdapter.messagesItems.add(data);
// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {
notifyMyAdapter();
}
});
}
}
}
}, filter);
}
I think all you have to do is while publishing the message data.setCreatedAtTime(System.currentTimeMillis();
the good thing about System.currentTimeMillis(); is that it is in epoch and millisecondsformat and would remain constant over all timeZones.
now on the reciever side you can convert the milliseconds into which ever format you desire
I have an application android who send data to the server (PC) but I don't receive any data from the PC to the application. And, how can I do a listener for incoming UDP messages ?
because I need an app to be running all the time even if the app is closed. How would I ensure that my 'listener' service is always running?
I would like to receive a notification when a message arrive from the server to the smartphone.
Here is my code :
public class MainActivity extends Activity implements TextWatcher, OnClickListener {
Client client;
EnvoyerMess envoyermessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
client = new Client();
client.execute();
lancer.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
envoyermessage=new EnvoyerMess();
envoyermessage.execute();
}
});
}
class Client extends AsyncTask<Void,Void,String>{
DatagramSocket client;
String test ;
public String doInBackground(Void...params){
String result=null;
int port=4000;
DatagramSocket clientSocket = null;
byte[] receiveData = new byte[256];
DatagramPacket packet = new DatagramPacket(receiveData, receiveData.length);
try{
InetAddress adresse = InetAddress.getByName("10.0.2.2");
clientSocket = new DatagramSocket(port);
clientSocket.receive(packet);
result = new String(packet.getData());
Log.d("","Received :) ");
}catch (Exception e) {
e.printStackTrace();
} finally {
if (clientSocket != null) {
clientSocket.close();
}
}
return result;
}
public void onPostExecute(String result){
if (result != null) {
//createNotify();
TextView tv = (TextView) MainActivity.this.findViewById(R.id.textView1);
tv.setText(result);
}
}
thanks in advance..
I would start by ensuring that the PC is in fact sending the packets to the correct IP address. Do this by using wireshark, http://www.wireshark.org/ , or a similar tool.
To ensure that your task is running even though the application is is closed you need a service, http://developer.android.com/guide/components/services.html.
Edit:
I you are using an emulator you must first enable redirection in the avd router. This link provides instructions on how to accomplish this developer.android.com/tools/devices/emulator.html#redirection (I cant publish links so you'll have to copy-paste).
If you are developing in windows you would also need to enable telnet. This link describes the steps to accomplish that social.technet.microsoft.com/wiki/contents/articles/910.windows-7-enabling-telnet-client.aspx (again, copy-paste)
i am trying to implement pubsub using openfire server and asmack library.
i have configured my node in such a way that subscribers has to take the approval of publishers
now i am trying to read the Message packet using following code but i am not getting any packet. i want to read Message packet because i want to know whenever the subscribers send the request to publishers to get subscribe.
PacketTypeFilter filter = new PacketTypeFilter(org.jivesoftware.smack.packet.Message.class);
PacketListener myListener = new PacketListener(){
#Override
public void processPacket(Packet packet) {
// TODO Auto-generated method stub
Log.d("PACKET" , "READ");
if(packet instanceof org.jivesoftware.smack.packet.Message){
org.jivesoftware.smack.packet.Message msg = (org.jivesoftware.smack.packet.Message) packet;
Log.d("MY MESSAGE" , msg.toXML()+ "");
}
}
};
cxmpp.addPacketListener(myListener, filter);
All i want is to read the incoming Message Packets
If you have all the configuration bits and your pubsub component correctly working, then for getting a normal message you would do something like:
ConnectionConfiguration config = new ConnectionConfiguration("ADDRESS",PORT); // service name, also known as XMPP domain of the target server.
config.setServiceName(this.pubsubServiceAddress);
connection = new XMPPConnection(config);
connection.connect();
PacketFilter filter = new MessageTypeFilter(Message.Type.normal);
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message mes = (Message)packet;
// do your stuff here
}
}, filter);
I am trying to use smack API for Android to develop a chat room using gmail accounts. I managed to create a room and send messages to group. But when someone replies i am not able to receive incoming messages either directly on chat window or through program(muc.addMessageListener(myMessageListener) and PacketListener).
ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
config.setSASLAuthenticationEnabled(true);
connection = new XMPPConnection(config);
connection.connect();
connection.login("kandroid6990#gmail.com", "password");
muc = new MultiUserChat(connection, "private-chat-12325669-dead-beff-feed-fedcba987454#groupchat.google.com");
muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
muc.join("kandroid6990");
muc.invite("karthikbaskar3#gmail.com/User1", "Welcome!");
muc.invite("kamaraj6990#gmail.com/User2", "Welcome!");
Message msg = new Message("private-chat-12325669-dead-beff-feed-fedcba987454#groupchat.google.com", Message.Type.groupchat);
msg.addBody(Message.getDefaultLanguage(), messageText);
muc.sendMessage(msg);
Try
muc.addMessageListener(myMessageListener);
private MultiUserChat muc; /* Initialize muc */
private void listeningForMessages()
{
muc.addMessageListener(new PacketListener() {
public void processPacket(Packet packet)
{
final Message message = (Message) packet;
// Do your action with the message
}
});
}
try this it may help in receive group chat message
Just implement group chat listener
PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
XmppTool.con.addPacketListener(new PacketListener() {
#Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
}
}
}
}, filter);