我已在我的应用程序中集成了quickblox聊天功能,并希望维护用户的在线和离线状态,并尝试了以下选项。
1)如文档中所述,我已按照此链接https://quickblox.com/developers/SimpleSample-users-android#Online.5COffline_status但有一个问题是它没有说明用户在线状态
2)使用了Ping Manager:https://quickblox.com/developers/Android_XMPP_Chat_Sample#Ping_a_user 但它总是给出QBResponseException
3)QBRoaster:此选项不适合作为要求。
[注意:我使用的是quickblox版本3.3.1]
我已经实现了QBPingManager,如下所示
public void checkUserOnlineStatus(String mOpponentName) {
QBChatService.getInstance().getPingManager().setPingInterval(1);
Performer<QBUser> qbUser = QBUsers.getUserByLogin(mOpponentName);
qbUser.performAsync(new QBEntityCallback<QBUser>() {
@Override
public void onSuccess(QBUser qbUser, Bundle bundle) {
final int mOpponentUserId = qbUser.getId();
final QBPingManager pingManager = QBChatService.getInstance().getPingManager();
pingManager.pingServer(new QBEntityCallback<Void>() {
@Override
public void onSuccess(Void aVoid, Bundle bundle) {
pingManager.pingUser(mOpponentUserId, new QBEntityCallback<Void>() {
@Override
public void onSuccess(Void aVoid, Bundle bundle) {
Timber.tag(TAG).w("Opponent User is online ");
}
@Override
public void onError(QBResponseException e) {
Timber.tag(TAG).e("message(ping user): " + e.getMessage() + "\n localized message: " + e.getLocalizedMessage());
e.printStackTrace();
}
});
}
@Override
public void onError(QBResponseException e) {
Timber.tag(TAG).e("message (ping server): " + e.getMessage() + "\n localized message: " + e.getLocalizedMessage());
}
});
}
@Override
public void onError(QBResponseException e) {
Timber.tag(TAG).e("Error : " + e.getMessage() + "\n Message : " + e.getLocalizedMessage());
}
});
}
感谢您的任何帮助。
答案 0 :(得分:2)
在这里,我们将找到上次看到的时间/日期的在线/离线状态:-
要获取用户的在线/离线状态,您需要发送该消息并必须确认订阅请求
// to confirm subscription request
QBSubscriptionListener subscriptionListener = new QBSubscriptionListener() {
@Override
public void subscriptionRequested(int userId) {
try {
if (chatRoster != null)
chatRoster.confirmSubscription(userId);
} catch (SmackException.NotConnectedException e) {
} catch (SmackException.NotLoggedInException e) {
} catch (XMPPException e) {
} catch (SmackException.NoResponseException e) {
}
}
};
chatRoster = QBChatService.getInstance().getRoster(QBRoster.SubscriptionMode.mutual, subscriptionListener);
// to send subscription request
try {
chatRoster.subscribe(qbChatDialog.getRecipientId()); //getRecipientId is Opponent UserID
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
每当用户在线/离线时发送状态
QBPresence presence = new QBPresence(QBPresence.Type.online, "I am now available", 1, QBPresence.Mode.available);
try {
chatRoster.sendPresence(presence);
} catch (SmackException.NotConnectedException e) {
} catch (Exception e) {
e.printStackTrace();
}
获取对手用户的存在
QBPresence presence = chatRoster.getPresence(qbChatDialog.getRecipientId());
if (presence.getType() == QBPresence.Type.online) {
//online
}else{
//offline
}
标识何时更改了用户的在线状态
QBRosterListener rosterListener = new QBRosterListener() {
@Override
public void entriesDeleted(Collection<Integer> userIds) {
}
@Override
public void entriesAdded(Collection<Integer> userIds) {
}
@Override
public void entriesUpdated(Collection<Integer> userIds) {
}
@Override
public void presenceChanged(QBPresence presence1) {
try {
int userid = presence1.getUserId();
int recid = qbChatDialog.getRecipientId();//opponent user id
if (userid == recid) {
if (presence1.getType() == QBPresence.Type.online)
status.setText(getResources().getString(R.string.online));
else {
status.setText("");
String lastseen = getlastseen();
if (lastseen.length() > 0) {
status.setText(lastseen);
}
}
} else {
}
} catch (Exception e) {
}
}
};
获取离线用户的最新消息
private String getlastseen() {
String lastseen = "";
String appendstring = "";
try {
long lastUserActivity = QBChatService.getInstance().getLastUserActivity(qbChatDialog.getRecipientId()); //returns last activity in seconds or error
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, -(int) lastUserActivity);
String format = "dd MMM yyyy hh:mm a";
if (DateUtils.isToday(calendar.getTimeInMillis())) {
format = "hh:mm a";
appendstring = ChatActivity.this.getResources().getString(R.string.today) + ",";
} else if (isyesterday(calendar.getTimeInMillis())) {
format = "hh:mm a";
appendstring = ChatActivity.this.getResources().getString(R.string.yesterday) + ",";
} else if (Calendar.YEAR == Calendar.getInstance().YEAR)
format = "dd MMM hh:mm aa";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
lastseen = simpleDateFormat.format(calendar.getTime());
} catch (XMPPException.XMPPErrorException | SmackException.NotConnectedException e) {
} catch (Exception e) {
e.printStackTrace();
}
return appendstring + lastseen;
}
答案 1 :(得分:1)
您可以使用QuickBlox Android SDK的新功能,因为版本3.3.3称为&#39;最后一个活动&#39;。您可以调用QBChatService.getInstance()。getLastUserActivity(userId);结果你会得到: - 0如果对手在线或 - 几秒钟前对手在线或 - 如果用户新用户登录聊天,则会出错。