gpt4 book ai didi

android - 没有收到带有 smack 的离线消息

转载 作者:太空狗 更新时间:2023-10-29 14:41:21 24 4
gpt4 key购买 nike

我正在尝试使用 openfire、smack 和 android 的聊天应用程序,其中离线消息不起作用。如果两个用户都在线,则能够正确发送和接收消息。但是,如果用户 A 离线并且用户 B 发送消息,则用户 A 在线后不会收到 B 发送的消息。尝试了来自 stackoverflow 的可能解决方案,但没有一个有效。使用以下代码检索离线消息。

新线程(){ 公共(public)无效运行(){

            XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()

.setSocketFactory(SocketFactory.getDefault())

.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)

.setServiceName("123.456.0.854")//service name

.setHost("123.456.0.854") // host name

.setPort(5222) //port

.setUsernameAndPassword("phone", "admin")
.setConnectTimeout(40000)

.setCompressionEnabled(false).build();

connection = new XMPPTCPConnection(config);

try {
connection.connect();
connection.login("phone", "admin");
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Available");
try {
connection.sendStanza(presence);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}



OfflineMessageManager offlineMessageManager = new OfflineMessageManager(connection);

try {
System.out.println("Count is " +offlineMessageManager.getMessageCount());
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}

}
}.start();

最佳答案

经过一番努力,我已经解决了这个问题。在您的 openfire 管理页面中,转到“客户端设置”并将空闲时间从 360 秒(默认)减少到 1 秒(可能)。只有这样,当您断开与 Internet 的连接时,它才能检测到您处于离线状态,并将其余消息保留为离线状态。

@Override public void onNetworkConnectionChanged(boolean isConnected) {

if(isConnected){
new Thread() {

public void run() {
try {
XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
builder.setUsernameAndPassword("phone", "admin");
builder.setSendPresence(true);
builder.setServiceName(<Service name>);
builder.setHost(<Host name>);
builder.setResource("Test");
builder.setDebuggerEnabled(true);
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Available");
connection = new XMPPTCPConnection(builder.build());
connection.connect();
connection.login();
Presence presence123 = new Presence(Presence.Type.available);
presence123.setStatus("Available");
try {
connection.sendStanza(presence123);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
StanzaFilter filter = new AndFilter(new StanzaTypeFilter(Message.class));
PacketListener myListener = new PacketListener()
{
public void processPacket(Stanza stanza)
{
retrieveMessage(stanza,userType);
}
};
connection.addPacketListener(myListener, filter);
try {
connection.sendStanza(presence);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}

} catch (SmackException | XMPPException | IOException e) {
e.printStackTrace();
}


//return connection.isConnected();
}

}.start();

上面的工作正常并且能够检索离线消息。方法“retrieveMessage(stanza,userType);”用于处理传入的消息并更新适配器。确保在重新连接时将状态发送为“可用”。如果还有任何问题,请告诉我。

关于android - 没有收到带有 smack 的离线消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47858448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com