3

I use 2.1.1 Android SDK and want to notify user about new incoming messages received by application.

Here is the issue. I would like to notify user as he launches application, thus i would like to add listener at the stage of "user login" as it's shown in the snippet:

        QBAuth.createSession(user, new QBEntityCallbackImpl<QBSession>() {
        @Override
        public void onSuccess(QBSession session, Bundle args) {
            // login to Chat
            chatService.login(user, new QBEntityCallbackImpl() {
                @Override
                public void onSuccess() {
                    HERE I WOULD LIKE A CODE TO START LISTEN FOR ALL INCOMING MESSAGES

As per http://sdk.quickblox.com/android/com/quickblox/chat/listeners/QBMessageListenerImpl.html listener needs chat QBChat to initiate. But I would like to listen for all of the messages, not only within particular chat.

Long story short, how to implement a message listener to catch all messages addressed to logged in user?

NBayaman
  • 103
  • 8

1 Answers1

2

@Naveen Kumar

In my start activity i launch listener to catch XMPP connection.

    private void XMPPConnectionListener() {
    XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {

        @Override
        public void connectionCreated(XMPPConnection connection) {
            GlobalVar.XMPPConn = connection;
            ChatHelper.idleMessageListener(StartActivity.this);

        }
    });

}

Then I use GlobalVar.XMPPConn to catch packets and parse them:

    public static void idleMessageListener(final Activity activity) {
    PacketListener myListener = new PacketListener() {
        public void processPacket(final Packet packet) {
            final Integer userID = Integer.parseInt(returnIDFromPacket(packet.getFrom()));
            final String body = returnBodyFromPacket(packet.toString());
            if (!GlobalVar.currentOpponent.equals(userID) && !body.isEmpty()) {
                activity.runOnUiThread(new Runnable() {
                    public void run() {
                        QBUsers.getUser(userID, new QBEntityCallbackImpl<QBUser>() {
                            @Override
                            public void onSuccess(final QBUser user, Bundle args) {
                                sendNotification(activity, user, body);
                            }

                            @Override
                            public void onError(List<String> errors) {
                                Log.d(TAG, errors.toString());
                            }
                        });
                    }
                });

            }
        }
    };
    PacketFilter filter = new MessageTypeFilter(Message.Type.chat);

    if (GlobalVar.XMPPConn != null) {
        GlobalVar.XMPPConn.addPacketListener(myListener, filter);
    }
}

So logic is to catch the connection fired by QuickBlox and then attach a packet listener to it.

NBayaman
  • 103
  • 8
  • 2
    could you explain the code bit more ? Because I don't understand what does GlobalVar means or how do you call XMPPConnectionListener, etc., please explain more about the code. – Kamalakannan J Jul 29 '15 at 06:40