JXMPP & smack Android 库,获取消息传递状态
JXMPP & smack Android Library, Get Message Delivery Status
他们正在处理聊天应用程序并且一切正常,现在我想检查我的消息是否已被接收者发送或看到。为此,Em 使用 XMPP 库的 DeliveryReceiptRequest,我得到了消息的 RequestID。但 Em 不确定如何通过此 ID 获取状态。你能帮帮我吗?我想在接下来的几个小时内完成它。
想问什么就问什么。我会立即回复你。
private void sendMessage(final String messageId, final String jid, final String content, final int chatType, final boolean isSticker) {
new Thread(new Runnable() {
@Override
public void run() {
Message message;
Log.e("Send SMS" , "Step 4");
try {
message = new Message(JidCreate.from(jid), content);
String deliveryReceiptId = DeliveryReceiptRequest.addTo(message);
Log.e("Numan Special", "sendMessage: deliveryReceiptId for this message is: " + deliveryReceiptId);
manageBoBExtension(message);
} catch (XmppStringprepException e) {
e.printStackTrace();
Log.e("Send SMS" , "Step 7");
mListener.onError(e.getLocalizedMessage());
return;
}
message.setStanzaId(messageId);
sendMessageDependingOnType(message, jid, chatType);
}
private void manageBoBExtension(Message message) {
if (isSticker) {
Log.e("Send SMS" , "Step 5");
BoBHash bobHash = new BoBHash(Base64.encode(content), "base64");
message.addExtension(new BoBExtension(bobHash, null, null));
}
Log.e("Send SMS" , "Step 6");
}
}).start();
}
private void sendMessageDependingOnType(Message message, String jid, int chatType) {
if (chatType == Chat.TYPE_MUC_LIGHT) {
MultiUserChatLightManager manager = XMPPSession.getInstance().getMUCLightManager();
try {
MultiUserChatLight multiUserChatLight = manager.getMultiUserChatLight(JidCreate.from(jid).asEntityBareJidIfPossible());
multiUserChatLight.sendMessage(message);
} catch (XmppStringprepException | InterruptedException | SmackException.NotConnectedException e) {
mListener.onError(e.getLocalizedMessage());
} finally {
mListener.onMessageSent(message);
}
} else {
ChatManager chatManager = RoomsListManager.getInstance().getChatManager();
try {
chatManager.createChat(JidCreate.from(jid).asEntityJidIfPossible()).sendMessage(message);
} catch (InterruptedException | XmppStringprepException | SmackException.NotConnectedException e) {
mListener.onError(e.getLocalizedMessage());
} finally {
mListener.onMessageSent(message);
}
}
}
发送消息前添加 DeliveryReceiptRequest.addTo(yourMsg)
在另一端收到消息后使用 message.addExtension(new DeliveryReceipt(packet.getStanzaId))
发送另一个 xmpp 消息,然后通过实施 ReceiptReceivedListener
监听更新状态。也可以参考官方文档here
他们正在处理聊天应用程序并且一切正常,现在我想检查我的消息是否已被接收者发送或看到。为此,Em 使用 XMPP 库的 DeliveryReceiptRequest,我得到了消息的 RequestID。但 Em 不确定如何通过此 ID 获取状态。你能帮帮我吗?我想在接下来的几个小时内完成它。
想问什么就问什么。我会立即回复你。
private void sendMessage(final String messageId, final String jid, final String content, final int chatType, final boolean isSticker) {
new Thread(new Runnable() {
@Override
public void run() {
Message message;
Log.e("Send SMS" , "Step 4");
try {
message = new Message(JidCreate.from(jid), content);
String deliveryReceiptId = DeliveryReceiptRequest.addTo(message);
Log.e("Numan Special", "sendMessage: deliveryReceiptId for this message is: " + deliveryReceiptId);
manageBoBExtension(message);
} catch (XmppStringprepException e) {
e.printStackTrace();
Log.e("Send SMS" , "Step 7");
mListener.onError(e.getLocalizedMessage());
return;
}
message.setStanzaId(messageId);
sendMessageDependingOnType(message, jid, chatType);
}
private void manageBoBExtension(Message message) {
if (isSticker) {
Log.e("Send SMS" , "Step 5");
BoBHash bobHash = new BoBHash(Base64.encode(content), "base64");
message.addExtension(new BoBExtension(bobHash, null, null));
}
Log.e("Send SMS" , "Step 6");
}
}).start();
}
private void sendMessageDependingOnType(Message message, String jid, int chatType) {
if (chatType == Chat.TYPE_MUC_LIGHT) {
MultiUserChatLightManager manager = XMPPSession.getInstance().getMUCLightManager();
try {
MultiUserChatLight multiUserChatLight = manager.getMultiUserChatLight(JidCreate.from(jid).asEntityBareJidIfPossible());
multiUserChatLight.sendMessage(message);
} catch (XmppStringprepException | InterruptedException | SmackException.NotConnectedException e) {
mListener.onError(e.getLocalizedMessage());
} finally {
mListener.onMessageSent(message);
}
} else {
ChatManager chatManager = RoomsListManager.getInstance().getChatManager();
try {
chatManager.createChat(JidCreate.from(jid).asEntityJidIfPossible()).sendMessage(message);
} catch (InterruptedException | XmppStringprepException | SmackException.NotConnectedException e) {
mListener.onError(e.getLocalizedMessage());
} finally {
mListener.onMessageSent(message);
}
}
}
发送消息前添加 DeliveryReceiptRequest.addTo(yourMsg)
在另一端收到消息后使用 message.addExtension(new DeliveryReceipt(packet.getStanzaId))
发送另一个 xmpp 消息,然后通过实施 ReceiptReceivedListener
监听更新状态。也可以参考官方文档here