Quickblox Chat

时间:2016-01-22 07:40:54

标签: android quickblox

使用 Quickblox 聊天时,如何在关闭应用时销毁会话?所以当我下次启动应用程序时,它应该可以登录聊天。

我所尝试的是在我的MainActivity的 onDestroy 上调用 deleteSession ,但它似乎没有帮助。

@Override
public void onDestroy() {
    super.onDestroy();
    QBUsers.signOut(new QBEntityCallbackImpl() {
        @Override
        public void onSuccess() {
            Log.v("SESSION","Session logout");
            ChatService.getInstance().removeConnectionListener(chatConnectionListener);
            QBAuth.deleteSession(new QBEntityCallback<Void>() {
                @Override
                public void onSuccess(Void aVoid, Bundle bundle) {
                    Log.v("SESSION","Session destroyed");

                }

                @Override
                public void onSuccess() {
                    Log.v("SESSION","Session destroyed 2");
                    //i get success here, when i onDestroy is called

                }

                @Override
                public void onError(List<String> list) {
                    Log.v("SESSION",list.toString());
                }
            });
        }

        @Override
        public void onError(List errors) {
            Log.v("SESSIONERROR",errors.toString());
        }
    });
}

情况:

当我启动应用程序时,它会获得Quickblox会话并登录聊天。 但如果我关闭应用程序。下次当我启动应用程序时,它会获得 Quickblox 会话并被卡在那里。

我需要从任务管理器中删除应用程序并启动应用程序才能登录。

我重新开始时的SDK响应

01-23 23:04:07.017 21733-21733/? D/ChatService: Initialise QBChatService
01-23 23:04:07.027 21733-21733/? D/QBASDK: =========================================================
                                           === REQUEST ==== 66f6e80c-9319-48c6-8d55-374d83c715e2 ===
                                           REQUEST
                                               POST https://api.quickblox.com/session.json
                                           HEADERS
                                               QuickBlox-REST-API-Version=0.1.1
                                               QB-SDK=Android 2.3.3
                                           PARAMETERS
                                               application_id=xxxx
                                               auth_key=U48ZQsXad2OcBNa
                                               nonce=-855163362
                                               timestamp=1453570447
                                               user[login]=egun1234
                                               user[password]=egun1234
                                               signature=f3edc13692c8ad3d4aae90717be7c05890ea4780
                                           INLINE
                                               POST https://api.quickblox.com/session.json?application_id=xxxxx&auth_key=U48ZQsXad2OcBNa&nonce=-855163362&timestamp=1453570447&user[login]=egun1234&user[password]=egun1234&signature=f3edc13692c8ad3d4aae90717be7c05890ea4780
01-23 23:04:12.047 21733-21733/? D/QBASDK: *********************************************************
                                           *** RESPONSE *** 66f6e80c-9319-48c6-8d55-374d83c715e2 ***
                                           STATUS : 201 
                                           HEADERS
                                               Access-Control-Allow-Origin=*
                                               Cache-Control=max-age=0, private, must-revalidate
                                               Connection=keep-alive
                                               Content-Length=270
                                               Content-Type=application/json; charset=utf-8
                                               Date=Sat, 23 Jan 2016 17:34:01 GMT
                                               ETag="b6aee90833f76c7e670ffb4b24a8a7a5"
                                               QB-Token-ExpirationDate=2016-01-23 19:34:01 UTC
                                               QuickBlox-REST-API-Version=0.1.1
                                               Server=nginx/1.6.2
                                               Status=201 Created
                                               Strict-Transport-Security=max-age=15768000;
                                               X-Rack-Cache=invalidate, pass
                                               X-Request-Id=e42acfadd6cb14f5b396587c0d837551
                                               X-Runtime=0.035198
                                               X-UA-Compatible=IE=Edge,chrome=1
                                           BODY
                                               '{"session":{"_id":"56a3b989a0eb471c5a00000f","application_id":xxxx,"created_at":"2016-01-23T17:34:01Z","device_id":0,"nonce":-855163362,"token":"767c6cadc31c5e6c45ba64ef29d45a9b28a2bd0d","ts":1453570447,"updated_at":"2016-01-23T17:34:01Z","user_id":7748679,"id":42627}}'
01-23 23:04:12.127 21733-21757/? D/QBASDK: Connecting to chat..
01-23 23:04:15.127 21733-21757/? D/QBASDK: Connected. Login to chat, currentUser JID: 7748679-xxxxx, resource: 811ee375c6eff567

当我关闭应用并再次启动时,SDK响应(应用仍在任务管理器中)

01-23 23:14:40.617 21733-21733/? D/QBASDK: =========================================================
                                           === REQUEST ==== ed61d1eb-a18d-4ba9-b730-8b552e5e46bc ===
                                           REQUEST
                                               POST https://api.quickblox.com/session.json
                                           HEADERS
                                               QuickBlox-REST-API-Version=0.1.1
                                               QB-SDK=Android 2.3.3
                                               QB-Token=767c6cadc31c5e6c45ba64ef29d45a9b28a2bd0d
                                           PARAMETERS
                                               application_id=xxxxx
                                               auth_key=U48ZQsXad2OcBNa
                                               nonce=1359179285
                                               timestamp=1453571080
                                               user[login]=egun1234
                                               user[password]=egun1234
                                               signature=f99dda85c65db5aff212512939fed7408f14461e
                                           INLINE
                                               POST https://api.quickblox.com/session.json?application_id=xxxxx&auth_key=U48ZQsXad2OcBNa&nonce=1359179285&timestamp=1453571080&user[login]=egun1234&user[password]=egun1234&signature=f99dda85c65db5aff212512939fed7408f14461e
01-23 23:14:48.657 21733-21733/? D/QBASDK: *********************************************************
                                           *** RESPONSE *** ed61d1eb-a18d-4ba9-b730-8b552e5e46bc ***
                                           STATUS : 201 
                                           HEADERS
                                               Access-Control-Allow-Origin=*
                                               Cache-Control=max-age=0, private, must-revalidate
                                               Connection=keep-alive
                                               Content-Length=270
                                               Content-Type=application/json; charset=utf-8
                                               Date=Sat, 23 Jan 2016 17:44:37 GMT
                                               ETag="778f1e9b028ecb55f5d5aaf210f22c5f"
                                               QB-Token-ExpirationDate=2016-01-23 19:44:37 UTC
                                               QuickBlox-REST-API-Version=0.1.1
                                               Server=nginx/1.6.2
                                               Status=201 Created
                                               Strict-Transport-Security=max-age=15768000;
                                               X-Rack-Cache=invalidate, pass
                                               X-Request-Id=9d23abaa4c411a42bb466a65c9aa78bc
                                               X-Runtime=0.041888
                                               X-UA-Compatible=IE=Edge,chrome=1
                                           BODY
                                               '{"session":{"_id":"56a3bc05a0eb47f007000097","application_id":xxxxx,"created_at":"2016-01-23T17:44:37Z","device_id":0,"nonce":1359179285,"token":"3fdf2abc49d381c5827db4647bade2c51db9b64d","ts":1453571080,"updated_at":"2016-01-23T17:44:37Z","user_id":7748679,"id":42501}}'

2 个答案:

答案 0 :(得分:1)

不要进行此类编码。

您在onDestroy()内进行异步请求。在这种情况下,即使在onDestroy()完全执行之后,onDestroy()内的代码也会继续运行(直到完成QBUsers.signOut()的完整执行),因为代码是异步运行的。因此,您需要将此代码置于按钮单击内,并且当signOut成功时,请退出您的应用程序。

答案 1 :(得分:1)

无需对会话执行任何操作或在 onDestroy 中登录 只需检查Splash Activity的 onResume 即可解决我的问题。

@Override
    protected void onResume() {
        super.onResume();
        if (isChatLoggedIn()) {
            decideLaunchActivity();
            finish();
        }
    }
    private boolean isChatLoggedIn() {
        return QBChatService.isInitialized() && QBChatService.getInstance().isLoggedIn();
    }