Android Pusher未连接到私人频道

时间:2016-07-04 10:59:21

标签: android pusher

我正在尝试在我的Android应用程序中实现推送聊天,我能够与推送器连接,得到响应为已连接。但无法连接到专用通道(本地服务器)。任何人都可以帮助这个。 添加以下代码。

提前致谢

final HttpAuthorizer authorizer = new HttpAuthorizer(myurl);
    authorizer.setHeaders(getMapAuthorizationHeaders());
    PusherOptions options = new PusherOptions().setAuthorizer(authorizer);
    final Pusher pusher = new Pusher("pusher_key", options);

    pusher.connect(new com.pusher.client.connection.ConnectionEventListener() {
        @Override
        public void onConnectionStateChange(ConnectionStateChange connectionStateChange) {

            Log.e("connectionStateChange" , connectionStateChange.getCurrentState().toString());

            if (connectionStateChange.getCurrentState() == ConnectionState.CONNECTED) {

                SOCKET_ID = pusher.getConnection().getSocketId();
                Log.e("SOCKET_ID" , ""+SOCKET_ID);


                Channel channel = pusher.subscribePrivate(PUSH_CHANNEL, new PrivateChannelEventListener() {
                    @Override
                    public void onAuthenticationFailure(String s, Exception e) {
                        Log.e("PUSHER", "Channel subscription authorization failed");
                    }

                    @Override
                    public void onSubscriptionSucceeded(String s) {
                        Log.e("PUSHER", "Channel subscription authorization succeeded");
                    }

                    @Override
                    public void onEvent(String s, String s2, String s3) {
                        Log.e("PUSHER", "An event with name " + s2 + " was delivered!!");
                    }
                }, "my-event");
            }
        }

        @Override
        public void onError(String s, String s1, Exception e) {

        }
    });



public static HashMap<String, String> getMapAuthorizationHeaders() {
try {
    HashMap<String, String> authHeader = new HashMap<>();
    authHeader.put("HeaderKey1", "HeaderValue1");
    authHeader.put("HeaderKey2", "HeaderValue2");
    return authHeader;

} catch (Exception e) {
    return null;
}

}

1 个答案:

答案 0 :(得分:0)

我可以假设您放置"pusher_key"的位置,您刚刚在此处添加了这个,并且在您的应用程序中,您实际上是在使用密钥吗?

除此之外,您在哪里定义PUSH_CHANNELpusher.subscribePrivate(PUSH_CHANNEL,