Android:如何使用quickblox维护用户在线/离线状态?

标签 android quickblox

我在我的应用程序中集成了 quickblox 聊天,并且想要维护用户在线和离线状态并尝试了以下选项。

1) 如文档中所述,我已点击此链接 https://quickblox.com/developers/SimpleSample-users-android#Online.5COffline_status但有一个问题是,它没有明确说明用户的在线状态

2) 使用 Ping 管理器: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());
            }
        });
      }

感谢任何帮助。

最佳答案

Here we will find online/offline status with last seen time/date :-

要获取用户的在线/离线状态,您需要发送并确认订阅请求

// 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;
    }

关于Android:如何使用quickblox维护用户在线/离线状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44885113/

相关文章:

java - 如何检测 MainActivity 是由应用程序启动启动的,还是由另一个 Activity 的 Intent 启动的?

java - 即使用户切换应用程序,CountDownTimer 也会运行

Android QuickBlox getFile 响应问题

ios - QBChatDelegate-chatDidReceiveMessage : getting called but dialogID is NULL

node.js - 如何在 Quickblox 聊天应用程序中显示用户状态(在线/离线)

java - 如何让用户登录应用程序?

javascript - 在 Phonegap 和 WebIntent 中使用 Android 推送通知

android - 如何检查 API 级别 28+ 支持哪些生物特征?

Android 应用程序在 Samsung Galaxy S3 上崩溃(内存不足错误)

ios - Quickblox:如何识别 QBActionStatusDelegate 中哪个推送通知发送失败