Notify when receive message XMPP + Smack - android

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

Related

get delivery status of recent messages in smack

I'm newby in smack 4.2.4 and xmpp. I've sent bunch of messages but the recipent is not available to get them,I've close the application and next time when I'll open the application want to check the status of messages, which is delivered or not.
You can use XEP-0184: Message Delivery Receipts for check delivery of messages to destination. First you must add the gradle dependency of smack-extensions:
implementation 'org.igniterealtime.smack:smack-extensions:4.2.2;
Then use this code, when you want to send a message, to add receipt request to stanza:
DeliveryReceiptRequest.addTo(message);
Then you can receive the delivery status in a listener like this:
DeliveryReceiptManager d = DeliveryReceiptManager.getInstanceFor(connection);
d.addReceiptReceivedListener(new ReceiptReceivedListener() {
#Override
public void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt) {
Log.i("delivery", "for: " + receiptId + " received");
//here you can use sid or receiptId to identify which message is delivered
}
});
Consider that when you are sending message, a random unique stanza-id (sid)
will be setted to your stanza. you must save it in your message row in database, then you can identify that with this sid when receipt received.

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.

how to add additional data in message asmack/smack?

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

Get Message body at login xmpp in android

How to get Message body at login xmpp in android?
I want to get offline message at login time in xmpp.
Any one have idea about. Any help would appreciated.
Thanks in advance.
This depends on your server. XEP-0160 recommends that the server store offline messages, and deliver them whenever the recipient sends presence with non-negative priority. If that's not what you're seeing, the first place to look is probably the server configuration.
Finally i got offline message at login in xmpp server.
connection.connect();
connection.login(username, password);
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
final Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message
.getFrom());
Log.i("XMPPClient", "Got text [" + message.getBody()
+ "] from [" + fromName + "]");
}
}
}, null);

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

Categories

Resources