我是推手新手。我已经在我的应用程序中成功实现了公共频道订阅。我目前停留在 Android 私人频道订阅上。
我们应该在服务器的 post 端点中传递请求正文参数。在我的 iOS 应用程序中,我们正在创建自定义授权程序以在 PusherOption 中发送请求正文。但在 Android 中,PusherOptions 中只有 2 个选项可以发送参数,例如 setHeaders() 和 setQueryStringParameters()。
我必须发送以下正文和 URL:
["data": ["socket_id": SOCKET_ID, "user_id": USER_ID, "channel": "private-CHANNEL_NAME"]]
我目前在 onAuthenticationFailure 处收到异常 [com.pusher.client.AuthorizationFailureException: java.io.FileNotFoundException: MY ENDPOINT URL]。
我当前的Android代码如下:
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 + "]");
}
});
}