How to implement message read status like whatsapp blue tick mark?

后端 未结 3 1755
日久生厌
日久生厌 2020-12-24 09:29

I am developing one application in that chatting is one module, for chatting i am using xmpp. when i am sending message i am getting that message delivery status using Deliv

相关标签:
3条回答
  • 2020-12-24 09:48

    Create a message with different attributes which indicate its a read receipt, which shall be sent when receiver reads the message. At the sender's end when you receive a message with read receipt attribute, then mark the message as read, as two blue ticks.

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

    Here status_id=101, I have used to send read receipt, which shall identify it at receiver end.

    0 讨论(0)
  • 2020-12-24 09:54

    You need to implement displayed when message is seen, this is basic why how messaging apps implements typing, sent, delivered and seen status

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

    0 讨论(0)
  • 2020-12-24 09:58

    create custom packet 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 enters the chat list send message tag with same packet id like this

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

    where mConnection is xmmppConnection object

    add packet extension to message object

    add this extension provider to ProviderManager before connecting to server

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

    create packetListener class to receive read receipt from receiver

    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 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);
    
    0 讨论(0)
提交回复
热议问题