How to implement message reading status, for example, whatsapp blue tick mark?

I develop one application in the chat, this is one module, I use xmpp for communication. when I send a message, I get this message delivery status using DeliveryReceiptManager.

DeliveryReceiptManager.getInstanceFor(connection).enableAutoReceipts(); DeliveryReceiptManager.getInstanceFor(connection).addReceiptReceivedListener(new ReceiptReceivedListener() { @Override public void onReceiptReceived(String arg0, String arg1, String arg2) { Log.v("app", arg0 + ", " + arg1 + ", " + arg2); } }); 

But I need to show that the message is a READ user or DO NOT like whatsApp blue tickmark, Can someone help me, I am amazed here. how to implement this concept of reading a message.

Thanks in advance.

+6
source share
3 answers

create your own package extension class

 public class ReadReceipt implements PacketExtension { public static final String NAMESPACE = "urn:xmpp:read"; public static final String ELEMENT = "read"; private String id; /// original ID of the delivered message public ReadReceipt(String id) { this.id = id; } public String getId() { return id; } @Override public String getElementName() { return ELEMENT; } @Override public String getNamespace() { return NAMESPACE; } @Override public String toXML() { return "<read xmlns='" + NAMESPACE + "' id='" + id + "'/>"; } public static class Provider extends EmbeddedExtensionProvider { @Override protected PacketExtension createReturnExtension(String currentElement, String currentNamespace, Map<String, String> attributeMap, List<? extends PacketExtension> content) { return new ReadReceipt(attributeMap.get("id")); } } } 

when entering a chat message, send a message tag with the same packet identifier as this

 Message message = new Message(userJid); ReadReceipt read = new ReadReceipt(messagePacketID); message.addExtension(read); mConnection.sendPacket(sendReadStatus); 

where mConnection is an xmmppConnection object

add package extension to message object

add this extension provider to the ProviderManager before connecting to the server

 ProviderManager.getInstance().addExtensionProvider(ReadReceipt.ELEMENT, ReadReceipt.NAMESPACE, new ReadReceipt.Provider()); 

create a packetListener class to receive confirmation from the recipient

 public class ReadReceiptManager implements PacketListener { private static Map<Connection, ReadReceiptManager> instances = Collections.synchronizedMap(new WeakHashMap<Connection, ReadReceiptManager>()); static { Connection.addConnectionCreationListener(new ConnectionCreationListener() { public void connectionCreated(Connection connection) { getInstanceFor(connection); } }); } private Set<ReceiptReceivedListener> receiptReceivedListeners = Collections.synchronizedSet(new HashSet<ReceiptReceivedListener>()); private ReadReceiptManager(Connection connection) { ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection); sdm.addFeature(ReadReceipt.NAMESPACE); instances.put(connection, this); connection.addPacketListener(this, new PacketExtensionFilter(ReadReceipt.NAMESPACE)); } public static synchronized ReadReceiptManager getInstanceFor(Connection connection) { ReadReceiptManager receiptManager = instances.get(connection); if (receiptManager == null) { receiptManager = new ReadReceiptManager(connection); } return receiptManager; } @Override public void processPacket(Packet packet) { ReadReceipt dr = (ReadReceipt)packet.getExtension(ReadReceipt.ELEMENT, ReadReceipt.NAMESPACE); if (dr != null) { for (ReceiptReceivedListener l : receiptReceivedListeners) { l.onReceiptReceived(packet.getFrom(), packet.getTo(), dr.getId()); } } } public void addReadReceivedListener(ReceiptReceivedListener listener) { receiptReceivedListeners.add(listener); } public void removeRemoveReceivedListener(ReceiptReceivedListener listener) { receiptReceivedListeners.remove(listener); } } 

finally add a listener to your xmpp connection object, it works successfully

  ReadReceiptReceivedListener readListener = new ReadReceiptReceivedListener() { @Override public void onReceiptReceived(String fromJid, String toJid, String packetId) { Log.i("Read", "Message Read Successfully"); } }; ReadReceiptManager.getInstanceFor(connection).addReadReceivedListener(readListener); 
+15
source

You need to implement display when a message appears, this is the main reason why messaging applications implement input, sending, delivery and viewing status

http://xmpp.org/extensions/xep-0022.html#sect-idp643808

+1
source

Create a message with different attributes that indicate its receipt for reading, which should be sent when the receiver reads the message. At the end of the sender, when you receive a message with a read attribute, then mark the message as read, like two blue ticks.

 <message id='xxxxx' from='xxxxxxxx' to='xxxxxxxxx'> <status_id>101</status_id> </message> 

Here status_id = 101, I used to send a receipt on receipt, which should identify it at the end of the receiver.

0
source

Source: https://habr.com/ru/post/983669/


All Articles