如何实现像whatsapp blue tick mark这样的消息阅读状态?
How to implement message read status like whatsapp blue tick mark?
我正在开发一个应用程序,其中聊天是一个模块,为了聊天我正在使用 xmpp。当我发送消息时,我使用 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);
}
});
但我需要证明该消息是用户已读或未读,就像 whatsApp 蓝色勾号一样,
任何人都可以帮助我,我在这里感到震惊。如何实现此消息读取概念。
提前致谢。
创建一条具有不同属性的消息,指示其已读回执,当接收者阅读消息时应发送该消息。在发件人端,当您收到带有已读回执属性的消息时,将消息标记为已读,标记为两个蓝色勾号。
<message id='xxxxx' from='xxxxxxxx' to='xxxxxxxxx'>
<status_id>101</status_id>
</message>
这里status_id=101,我用的是发送已读回执,在接收端识别。
创建自定义数据包扩展 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"));
}
}
}
当进入聊天列表时发送具有相同数据包 ID 的消息标签,如下所示
Message message = new Message(userJid);
ReadReceipt read = new ReadReceipt(messagePacketID);
message.addExtension(read);
mConnection.sendPacket(sendReadStatus);
其中 mConnection 是 xmmppConnection 对象
向消息对象添加数据包扩展
在连接到服务器之前将此扩展提供程序添加到 ProviderManager
ProviderManager.getInstance().addExtensionProvider(ReadReceipt.ELEMENT, ReadReceipt.NAMESPACE, new ReadReceipt.Provider());
创建 packetListener class 以接收来自接收器的已读回执
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);
}
}
终于将侦听器添加到您的 xmpp 连接对象,它成功运行了
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);
您需要在看到消息时实现显示,这就是为什么消息传递应用程序如何实现输入、发送、传递和看到状态的基本原因
我正在开发一个应用程序,其中聊天是一个模块,为了聊天我正在使用 xmpp。当我发送消息时,我使用 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);
}
});
但我需要证明该消息是用户已读或未读,就像 whatsApp 蓝色勾号一样, 任何人都可以帮助我,我在这里感到震惊。如何实现此消息读取概念。
提前致谢。
创建一条具有不同属性的消息,指示其已读回执,当接收者阅读消息时应发送该消息。在发件人端,当您收到带有已读回执属性的消息时,将消息标记为已读,标记为两个蓝色勾号。
<message id='xxxxx' from='xxxxxxxx' to='xxxxxxxxx'>
<status_id>101</status_id>
</message>
这里status_id=101,我用的是发送已读回执,在接收端识别。
创建自定义数据包扩展 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"));
}
}
}
当进入聊天列表时发送具有相同数据包 ID 的消息标签,如下所示
Message message = new Message(userJid);
ReadReceipt read = new ReadReceipt(messagePacketID);
message.addExtension(read);
mConnection.sendPacket(sendReadStatus);
其中 mConnection 是 xmmppConnection 对象
向消息对象添加数据包扩展
在连接到服务器之前将此扩展提供程序添加到 ProviderManager
ProviderManager.getInstance().addExtensionProvider(ReadReceipt.ELEMENT, ReadReceipt.NAMESPACE, new ReadReceipt.Provider());
创建 packetListener class 以接收来自接收器的已读回执
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);
}
}
终于将侦听器添加到您的 xmpp 连接对象,它成功运行了
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);
您需要在看到消息时实现显示,这就是为什么消息传递应用程序如何实现输入、发送、传递和看到状态的基本原因