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);
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();
}
This is my code and i am sending my message in this method but not able to get delivery message in DeliveryReceipt
newChat.sendMessage(message);
newChat.getListeners();
newChat.addMessageListener(new ChatMessageListener() {
#Override
public void processMessage(Chat chat, Message message) {
Log.d(TAG,"DeliveryReceipt3:"+"1:"+message.getThread()+"2:"+chat.getThreadID()+"3:"+message.getBody());
}
});
the above code is for sending message and below code is for getting DeliveryReceipt
Message m = new Message();
m.setType(Message.Type.chat);
m.setFrom(connection.getUser());
m.setTo(agentId);
m.addExtension(new DeliveryReceipt(m.getPacketID()));
DeliveryReceipt dr = (DeliveryReceipt)m.getExtension(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE);
Log.d(TAG,"DeliveryReceipt1: "+"1:"+dr.getElementName()+"2:"+dr.getId()+"3:"+dr.getNamespace()+"4:"+dr.toXML()+"4:"+dr.toString());
Log.d(TAG,"DeliveryReceipt2: "+"1:"+m.getBody()+"2:"+m.getStanzaId()+"3:"+m.getThread());
you need to add DeliveryReceiptManager after you successfully logs in
DeliveryReceiptManager dm = DeliveryReceiptManager.getInstanceFor(connection);
dm.setAutoReceiptMode(AutoReceiptMode.always);
dm.autoAddDeliveryReceiptRequests();
dm.addReceiptReceivedListener(new ReceiptReceivedListener() {
#Override
public void onReceiptReceived(Jid fromJid, Jid toJid,
final String receiptId, Stanza receipt) {
// handle delivery receipt here
}
});
First add these to your configuration-
DeliveryReceiptManager.setDefaultAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);
ProviderManager.addExtensionProvider(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceipt.Provider());
ProviderManager.addExtensionProvider(DeliveryReceiptRequest.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceiptRequest.Provider());
Then after XMPPTcpConnection established add these-
DeliveryReceiptManager deliveryReceiptManager = DeliveryReceiptManager.getInstanceFor(this.connection);
deliveryReceiptManager.setAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);
deliveryReceiptManager.autoAddDeliveryReceiptRequests();
This will enable delivery receipt for all messages.
SENT:
<iq to='chat_host' id='o2huU-440' type='get'><query xmlns='http://jabber.org/protocol/disco#info'></query></iq>
RECV:
<iq from='chat_host'id='o2huU-440' to='userJID'type='result'><query xmlns='http://jabber.org/protocol/disco#info'> <feature var='urn:xmpp:receipts'/></query>
For auto receipt mode all message will be sent with a receipt request extension. And you will get delivery receipt for all messages.
I am sending a message with (a)Smack and Openfire server. I am successfully able to send message with the message body. Now i need to send some additional data with the message. I don't want to append the string to the data and then process it after receiving. Is there any other approach? or with extensions?
Use a custom PacketExtension.
You can use setProperty and getProperty method.
At sending end:
Message msg=new Message("jid", Message.Type.chat);
msg.setProperty("key", "value");
connection.sendMessage(msg);
At receiving end:
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
packetListner=new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
String myData=message.getProperty("key").toString();
}
};
connection.addPacketListener(packetListner, filter);
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 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();