Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pusher Private Channel Subscription in android

Tags:

android

pusher

I have issue on private channel subscription in android. These is the my code

        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization", code);
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        headers.put("Accept", "application/json");

        final HttpAuthorizer authorizer = new HttpAuthorizer("My URL");

        authorizer.setHeaders(headers);

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

        final Pusher pusher = new Pusher("KEY", options);

        pusher.connect(new ConnectionEventListener() {
            @Override
            public void onConnectionStateChange(ConnectionStateChange change) {

            }

            @Override
            public void onError(String message, String code, Exception e) { }
        }, ConnectionState.ALL);

        PrivateChannel channel = pusher.subscribePrivate(channelName);
        channel.bind("message-sent", new PrivateChannelEventListener() {
            @Override
            public void onAuthenticationFailure(String string, Exception ex) {}
            @Override
            public void onSubscriptionSucceeded(String string) {
            }

            @Override
            public void onEvent(String string, String string1, String string2) {}});
pusher.connect();

This create success connection but in subscription it did return any result. Please help me to solve this issue.

like image 272
ayesh don Avatar asked Oct 18 '22 12:10

ayesh don


1 Answers

I think your issue comes from bind() being called before before the channel is connected.

Can you try putting the subscribe and bind calls inside the callbacks, similar to this (I moved them to methods so it's easier to follow):

Pusher pusher;
PrivateChannel channel;

void subscribeToChannel() {
    channel = pusher.subscribePrivate(channelName, new PrivateChannelEventListener() {
        @Override
        public void onSubscriptionSucceeded(String s) {
            bindToEvents();
        }
        @Override
        public void onAuthenticationFailure(String s, Exception e) {}
        @Override
        public void onEvent(String s, String s1, String s2) {}
    });
}

void bindToEvents() {
    channel.bind("message-sent", new PrivateChannelEventListener() {
        @Override
        public void onAuthenticationFailure(String string, Exception ex) {}
        @Override
        public void onSubscriptionSucceeded(String string) {}
        @Override
        public void onEvent(String string, String string1, String string2) {}});
}

And in the main method you can just call pusher.connect() that will call subscribeToChannel() when connected successfully:

    HashMap<String, String> headers = new HashMap<>();
    headers.put("Authorization", code);
    headers.put("Content-Type", "application/x-www-form-urlencoded");
    headers.put("Accept", "application/json");
    final HttpAuthorizer authorizer = new HttpAuthorizer("My URL");
    authorizer.setHeaders(headers);

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

    final Pusher pusher = new Pusher("KEY", options);

    pusher.connect(new ConnectionEventListener() {

        @Override
        public void onConnectionStateChange(ConnectionStateChange change) {

            if (change.getCurrentState() == ConnectionState.CONNECTED) {
                subscribeToChannel();
            }
        }

        @Override
        public void onError(String s, String s1, Exception e) {}
    });
like image 141
zmarkan Avatar answered Oct 21 '22 03:10

zmarkan