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
Related
I am trying to fetch the chat history using below function:
var mamManager:MamManager= MamManager.getInstanceFor(connection)
var prevMsg=mamManager.queryArchive(JidCreate.entityBareFrom(jid)).forwardedMessages
println(msg.forwardedStanza)
But I am getting the output as
04-19 14:17:58.414 7964-7964/com.example.itstym.smackchat I/System.out: Message Stanza [to=user2#replica3377.cloudapp.net,from=user1#replica3377.cloudapp.net/307135084148417199024926,id=qQ73x-27,]
I can also get stanza id using msg.forwardedStanza.stanzaId but how will i get the msg text.
Cast the forwarded Stanza to Message and call getBody().
The getBody() is for Message and the ForwardedStanza extends Stanza so it can be Message, IQ or Presence.
Try this:
if (forwarded.getForwardedStanza() instanceof Message) {
String body = ((Message) forwarded.getForwardedStanza()).getBody();
}
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'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());
}
}
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 on last Steps of my Chat app, i'm trying to receive Notification whenever Messages are Delivered, so i found this implementation:
MessageEventRequestListener
which comes with 4 overrides:
#Override
public void deliveredNotificationRequested(String from, String packetID,
MessageEventManager messageEventManager) {
System.out.println("delievered");
}
#Override
public void displayedNotificationRequested(String from, String packetID,
MessageEventManager messageEventManager) {
System.out.println("displaydelievered");
}
#Override
public void composingNotificationRequested(String from, String packetID,
MessageEventManager messageEventManager) {
System.out.println("composingdelievered");
}
#Override
public void offlineNotificationRequested(String from, String packetID,
MessageEventManager messageEventManager) {
System.out.println("offlinedelievered");
}
but when a message is sent, nothing happened, i'm wondering am i missing any thing? is this the right approach ? thanks
As #Flow said,XEP-22: Message events is deprecated.You can use DeliveryReceiptManager.
The listener invoked when message received at receiver side
DeliveryReceiptManager.getInstanceFor(connection).addReceiptReceivedListener(new ReceiptReceivedListener()
{
#Override
public void onReceiptReceived(String arg0, String arg1, String arg2) {
Log.i("Message Delivered To=", arg0 + ", From = " + arg1 + " , ID= " + arg2);
}
});
}
while sending message add following code
//add delivery receipt request
DeliveryReceiptManager.addDeliveryReceiptRequest(Message);
In PacketListener add following code
Packet received = new Message();
received.addExtension(new DeliveryReceipt(packet.getPacketID()));
received.setTo(packet.getFrom());
connection.sendPacket(received);
XEP-22: Message events is deprecated, I don't recommend using it. Use Delivery Receipts (XEP-0184) instead: https://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smackx/receipts/DeliveryReceiptManager.html
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.
For each message you send, you may send notifications requests with :
MessageEventManager.addNotificationsRequests(message,
true, true, true, true);
use this code where you get packet of message.
//checking is deliverd or not.
if(msg.getBody().toString().equalsIgnoreCase("RecivedByReciver")){
//do what you want after get notify.
}else{
//do what you want if not delevery report message.
Message message=new Message(ConnectionManager.parseBareAddress(msg.getFrom()),Message.Type.chat);
message.addBody(null,"RecivedByReciver");
Connection().sendPacket(message);
}