labreu
labreu

Reputation: 444

Pusher for Android Implementation

I'm trying to implement a pusher service in my Android app, doesn't have access to the server just copying from an iOS app previous implementation. Everything works fine in connection process but when subscribe to a private channel the authentication fails with:

"com.pusher.client.AuthorizationFailureException: java.io.FileNotFoundException: https://authorization_url"

The implementation goes like this:

    HttpAuthorizer authorizer = new HttpAuthorizer(PUSHER_AUTH_URL);
    PusherOptions options = new PusherOptions().setEncrypted(true).setWssPort(443).setAuthorizer(authorizer);
    pusher = new Pusher(PUSHER_KEY, options);
    pusher.connect(new com.pusher.client.connection.ConnectionEventListener() {
                    @Override
                    public void onConnectionStateChange(ConnectionStateChange change) {

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

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

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

                    @Override
                    public void onError(String message, String code, Exception e) {
                        Log.w("PUSHER", "There was a problem connecting with code " + code + " and message " + message);
                    }
                }, ConnectionState.ALL);

UPDATE

I'm sure that the problem is with the authentication, there is a function call in iOS version that set some headers to the channel subscription or something like that:

(void)pusher:(PTPusher *)pusher willAuthorizeChannel:(PTPusherChannel *)channel withRequest:(NSMutableURLRequest *)request;
{
    [request addAuthorizationHeadersForUser:self.credentials.user];

}

Im trying to figure out where to add the headers in android, try to add it to the authorizer but nothing change:

authorizer.setHeaders(addMapAuthorizationHeaders());

Any idea of what is the equivalent in Android of that iOS function: willAuthorizeChannel??

Upvotes: 1

Views: 4057

Answers (2)

labreu
labreu

Reputation: 444

Ok solved, it was what I thought, the HttpAuthorizer needed a set of headers that you can set directly when creating it like:

HttpAuthorizer authorizer = new HttpAuthorizer(PUSHER_AUTH_URL);
authorizer.setHeaders(MY_AUTH_HEADERS); //a HashMap with the headers
PusherOptions options = new PusherOptions().setEncrypted(true).setWssPort(443).setAuthorizer(authorizer);
pusher = new Pusher(PUSHER_KEY, options);

And with that works fine, in case somebody have a similar problem.

EDIT: this is how to set the authorization headers. It's a Map set to "Key" "Value" pair for example:

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;
    }
}

So the pusher config will be like:

authorizer.setHeaders(getMapAuthorizationHeaders());

Upvotes: 3

Catalin
Catalin

Reputation: 1899

I've been struggling with this as well... the solution is simple.

First check this out: https://github.com/pusher/pusher-websocket-java/blob/master/src/main/java/com/pusher/client/util/HttpAuthorizer.java

Then implement the abstract interface Authorizer and override the authorize method with your own code and that's it, you get the same thing as on the iOS.

Some snippet to get you started (with a custom constructor):

CustomSocketHttpAuthorizer authorizer = new CustomSocketHttpAuthorizer(ServerComm.API_MAIN_LINK + ServerComm.API_LINK_PUSHER_AUTH, pusherServerAuthTimeStamp, MessageActivity.this);

PusherOptions options = new PusherOptions().setAuthorizer(authorizer).setEncrypted(true);;

clientPusher = new Pusher(ServerComm.PUSHER_CLIENT_KEY, options);

clientPusher.connect(new ConnectionEventListener() .....

Upvotes: 1

Related Questions