问题
I am new to Pusher. I had successfully implemented public channel subscription in my app. I am currently stuck at Android private channel subscription.
We are supposed to pass request body parameters in post endpoint of our server. In my iOS application, We are creating custom Authorizer to send the request body in the PusherOption. But in Android there are only 2 options to send parameters in PusherOptions like setHeaders() and setQueryStringParameters().
I have to send following body with the URL:
["data": ["socket_id": SOCKET_ID, "user_id": USER_ID, "channel": "private-CHANNEL_NAME"]]
I am currently getting exception at onAuthenticationFailure as [com.pusher.client.AuthorizationFailureException: java.io.FileNotFoundException: MY ENDPOINT URL].
My current Android code is as follows:
public static void subscribePrivateChannel(final String channelName, final String userId, final String authKey) {
final HttpAuthorizer authorizer = new HttpAuthorizer(MY_ENDPOINT_URL);
PusherOptions options = new PusherOptions().setEncrypted(true).setAuthorizer(authorizer);
final Pusher pusher = new Pusher(Config.PUSHER_APP_KEY, options);
pusher.connect(new ConnectionEventListener() {
@Override
public void onConnectionStateChange(ConnectionStateChange connectionStateChange) {
if (connectionStateChange.getCurrentState().toString().equalsIgnoreCase("CONNECTED")) {
String socketId = pusher.getConnection().getSocketId();
HashMap<String, String> parameters = new HashMap<>();
parameters.put("socket_id", socketId);
parameters.put("channel", channelName);
parameters.put("user_id", userId);
authorizer.setQueryStringParameters(parameters);
pusher.subscribePrivate(channelName, new PrivateChannelEventListener() {
@Override
public void onAuthenticationFailure(String s, Exception e) {
Log.d(TAG, "onAuthenticationFailure() called with: s = [" + s + "], e = [" + e + "]");
}
@Override
public void onSubscriptionSucceeded(String s) {
Log.d(TAG, "onSubscriptionSucceeded() called with: s = [" + s + "]");
}
@Override
public void onEvent(String s, String s1, String s2) {
Log.d(TAG, "onEvent() called with: s = [" + s + "], s1 = [" + s1 + "], s2 = [" + s2 + "]");
}
});
}
}
@Override
public void onError(String s, String s1, Exception e) {
Log.d(TAG, "onError() called with: s = [" + s + "], s1 = [" + s1 + "], e = [" + e + "]");
}
});
}
来源:https://stackoverflow.com/questions/42952078/android-pusher-request-body-implementation-in-httpauthorizer