how to add additional data in message asmack/smack? - android

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);

Related

how to get delivery status of message in smack or in xmpp from android

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.

Notify when receive message XMPP + Smack

I'm just working with Smack and XMPP server and has problem when receive message from another user.
I have 2 user, when user1 send message to user2, user2 will be notified about new message.
In my app, I can see log about message received such as:
11-06 14:50:03.266 18753-19058/com.example.xmppdemo D/SMACK: RECV (0): <message id='05EW4-32' type='chat' to='off1#***' from='off2#***/Smack'><body>hhh</body></message>
But I do not know how to handle this message to show notify for user2 ?
Anyone know it ? Help me please
Thanks
You can try :
chatListner = new PacketListener()
{
public void processPacket(Packet packet)
{
printLog("packet got");
Message message = (Message) packet;
printLog("packet subject" + message.getSubject());
printLog("packet body" + message.getBody());
printLog("packet id"+message.getPacketID());
}
}

Packet id is null in the messages received from QuickBlox chatroom

Below is my Android code for group chat:
/* Sends messages */
public void sendChatMessage(String text)
{
Message message = muc.createMessage();
message.setBody(text);
message.setPacketID("ABC_1");
muc.sendMessage(message);
}
/* Listens for messages */
public void receiveMessage()
{
muc.addMessageListener(new PacketListener() {
public void processPacket(Packet packet)
{
final Message message = (Message) packet;
log.i("packed id: ", packet.getPacketID());
log.i("message id: ", message.getPacketID());
}
});
}
The output I see in logcat is:
packet id: null
message id: null
Could you please tell me why the IDs are null? Am I missing something here?
it's OK
Regarding XMPP standart, room doesn't handle message id, so it will be null
Here is an example
sent message to room:
<message to="92_testroom2134#muc.chat.quickblox.com" id="366" type="groupchat"><body>Hello QuickBlox developer!</body></message>
received message from room:
<message xmlns="jabber:client" type="groupchat" from="92_testroom2134#muc.chat.quickblox.com/298" to="298-92#chat.quickblox.com/tigase-19912"><body>Hello QuickBlox developer!</body></message>
no ID in received message
ID only used in 1-1 chat
you can add custom parameters to Messages, just use http://www.igniterealtime.org/builds/smack/docs/3.2.2/javadoc/org/jivesoftware/smack/packet/Packet.html#setProperty(java.lang.String, java.lang.Object) to set
and http://www.igniterealtime.org/builds/smack/docs/3.2.2/javadoc/org/jivesoftware/smack/packet/Packet.html#getProperty(java.lang.String)
to get in listener

read incoming Message packets in pubsub using smack

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);

Not able to receive chat messages using smackx MultiUserChat API

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);

Categories

Resources