0

I've already looked over many posts in StackOverflow and their git issues. The problem with almost all of them is about header for HttpAuthorizer. But it's still not working for me. Can anyone help me to find what's wrong with my code? Thank you.

Note: I don't know if the error is on the server side, because when my team tested it with simple chat app from browser it works.

public class PusherService {
    private static final String CHANNEL = "private-User." + accountId;

    private static PusherService instance;
    private Pusher pusher;

    public static synchronized PusherService getInstance() {
        if (instance == null) {
            instance = new PusherService();
        }

        return instance;
    }

    private PusherService() {
        pusher = initiatePusher();
    }

    private static Pusher initiatePusher() {
        HttpAuthorizer httpAuthorizer = new HttpAuthorizer(BuildConfig.PUSHER_AUTH_URL);
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", Session.getToken());
        headers.put("Accept", "application/json");
        headers.put("Content-Type", "application/json");
        httpAuthorizer.setHeaders(headers);

        PusherOptions options = new PusherOptions()
                .setCluster(BuildConfig.PUSHER_CLUSTER)
                .setEncrypted(true)
                .setAuthorizer(httpAuthorizer);

        Pusher pusher = new Pusher(BuildConfig.PUSHER_KEY, options);
        pusher.connect(new ConnectionEventListener() {
            @Override
            public void onConnectionStateChange(ConnectionStateChange connectionStateChange) {
                Log.i("Pusher", "Connection State Change: " + connectionStateChange.getCurrentState().name());
            }

            @Override
            public void onError(String s, String s1, Exception e) {
                Log.i("Pusher", String.format("Connection Error: [%s], exception: [%s]", s, e));
            }
        });

        return pusher;
    }

    public PrivateChannel getChannel() {
        PrivateChannel privateChannel = pusher.getPrivateChannel(CHANNEL);
        if (privateChannel == null) {
            privateChannel = pusher.subscribePrivate(CHANNEL, new PrivateChannelEventListener() {
                @Override
                public void onAuthenticationFailure(String s, Exception e) {
                    // Authentication failed
                    Log.i("Pusher", String.format("Authentication failure due to [%s], exception: [%s]", s, e));
                }

                @Override
                public void onSubscriptionSucceeded(String s) {
                    Log.i("Pusher", "Private connection succeeded");
                }

                @Override
                public void onEvent(String channel, String event, String data) {
                    Log.i("Pusher", data);
                }
            });
        }

        return privateChannel;
    }
}

Then to test it I just called

PusherService.getInstance().getChannel();

Dark Leonhart
  • 1,494
  • 2
  • 13
  • 21

1 Answers1

0

After 2 days searching, we found the answer. I just need to remove the Content-Type header.

Dark Leonhart
  • 1,494
  • 2
  • 13
  • 21