Android Pubnub一对一聊天

时间:2017-03-25 18:13:37

标签: android pubnub

我想使用android中的pubnub库实现Online / Offline / IsTying功能。为此,我创建了Pubnub连接。

//create the pubnub connection

 PNConfiguration configuration = new PNConfiguration();
            configuration.setPublishKey(PUBNUB_PUBLISH_KEY);
            configuration.setSubscribeKey(PUBNUB_SUBSCRIBE_KEY);
            configuration.setLogVerbosity(PNLogVerbosity.BODY);
            configuration.setConnectTimeout(100000);
            configuration.setSubscribeTimeout(31000);
            configuration.setHeartbeatNotificationOptions(PNHeartbeatNotificationOptions.ALL);
            configuration.setPresenceTimeoutWithCustomInterval(120,59);
            configuration.setPresenceTimeout(120);
            mPubNub = new PubNub(configuration);

然后我将订阅者回调设置为监听器

**strong text**

private SubscribeCallback subscribeCallback = new SubscribeCallback() {

        @Override
        public void status(PubNub pubnub, PNStatus status) {
            Log.d("Chat", "status() called with: pubnub = [" + pubnub + "], status = [" + status + "]");
            if (status.getOperation() != null) {
                switch (status.getOperation()) {

                    // let's combine unsubscribe and subscribe handling for ease of use
                    case PNSubscribeOperation:
                    case PNUnsubscribeOperation:
                        Toast.makeText(ChatAppService.this, "Status : " + status.getOperation().name(), Toast.LENGTH_SHORT).show();
                        // note: subscribe statuses never have traditional
                        // errors, they just have categories to represent the
                        // different issues or successes that occur as part of subscribe
                        switch (status.getCategory()) {
                            case PNConnectedCategory:
                                // this is expected for a subscribe, this means there is no error or issue whatsoever
                                Toast.makeText(ChatAppService.this, "Status : " + status.getCategory(), Toast.LENGTH_SHORT).show();
                                break;
                            case PNReconnectedCategory:
                                // this usually occurs if subscribe temporarily fails but reconnects. This means
                                // there was an error but there is no longer any issue
                                Toast.makeText(ChatAppService.this, "Status : " + status.getCategory(), Toast.LENGTH_SHORT).show();
                                HashMap<String,String> map = new HashMap();
                                map.put("State","Online");
                                pubnub.setPresenceState().channels(Arrays.asList(Constants.GLOBAL_CHANNEL)).state(map).uuid(pubnub.getConfiguration().getUuid());
                                break;
                            case PNDisconnectedCategory:
                                // this is the expected category for an unsubscribe. This means there
                                // was no error in unsubscribing from everything
                                Toast.makeText(ChatAppService.this, "Status : " + status.getCategory(), Toast.LENGTH_SHORT).show();
                                HashMap<String,String> mapOffline = new HashMap();
                                mapOffline.put("State","Offline");
                                pubnub.setPresenceState().channels(Arrays.asList(Constants.GLOBAL_CHANNEL)).state(mapOffline).uuid(pubnub.getConfiguration().getUuid());
                                break;
                            case PNTimeoutCategory:
                                HashMap<String,String> mapTimeout = new HashMap();
                                mapTimeout.put("State","Offline");
                                pubnub.setPresenceState().channels(Arrays.asList(Constants.GLOBAL_CHANNEL)).state(mapTimeout).uuid(pubnub.getConfiguration().getUuid());
                                pubnub.reconnect();
                                break;
                            case PNUnexpectedDisconnectCategory:
                                // this is usually an issue with the internet connection, this is an error, handle appropriately
                                Toast.makeText(ChatAppService.this, "Status : " + status.getCategory(), Toast.LENGTH_SHORT).show();
                                pubnub.reconnect();
                                break;
                            case PNAccessDeniedCategory:
                                // this means that PAM does allow this client to subscribe to this
                                // channel and channel group configuration. This is another explicit error
                                Toast.makeText(ChatAppService.this, "Status : " + status.getCategory(), Toast.LENGTH_SHORT).show();
                                break;
                            default:
                                // More errors can be directly specified by creating explicit cases for other
                                // error categories of `PNStatusCategory` such as `PNTimeoutCategory` or `PNMalformedFilterExpressionCategory` or `PNDecryptionErrorCategory`
                        }
                        break;
                    case PNHeartbeatOperation:
                        // heartbeat operations can in fact have errors, so it is important to check first for an error.
                        // For more information on how to configure heartbeat notifications through the status
                        // PNObjectEventListener callback, consult <link to the PNCONFIGURATION heartbeart config>
                        if (status.isError()) {
                            // There was an error with the heartbeat operation, handle here
                        } else {
                            // heartbeat operation was successful
                        }
                        break;
                    default: {
                        // Encountered unknown status type
                    }
                }
            } else {
                // After a reconnection see status.getCategory()
            }

        }

        @Override
        public void message(PubNub pubnub, PNMessageResult message) {
            //TODO: If App open: Save to database and broadcast for database change else show Notification

        }

        @Override
        public void presence(PubNub pubnub, PNPresenceEventResult presence) {
            Log.d("chat", "presence() called with: pubnub = [" + pubnub + "], presence = [" + presence + "]");
        }
    };`

最后问题是,当我离开小组时,没有出现回叫。我应该得到离开频道的用户状态的事件,以便我可以从在线用户的列表中删除用户。

取消订阅论坛

pubnub.unsubscribe().channels(Arrays.asList(channel)).execute()

当我订阅频道时,在线状态回调会调用三次 1.事件:加入 2.事件:状态变化 3.事件:离开

我没有得到如何在Android中处理这种情况。请帮助我,我在过去三天里遇到了这个问题。

0 个答案:

没有答案