在Android中推送私人频道订阅

时间:2017-05-22 11:58:03

标签: android pusher

我在android中的私人频道订阅上有问题。 这些是我的代码

        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();

这创建成功连接但是在订阅中确实返回了任何结果。 请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我认为您的问题来自于在连接频道之前调用bind()。

你能尝试在回调中放置subscribe和bind调用,类似于这个(我将它们移到方法中,因此更容易理解):

/*
 * Fill the binprm structure from the inode.
 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
 *
 * This may be called multiple times for binary chains (scripts for example).
 */
int prepare_binprm(struct linux_binprm *bprm)
{
    int retval;

    bprm_fill_uid(bprm);

    /* fill in binprm security blob */
    retval = security_bprm_set_creds(bprm);
    if (retval)
        return retval;
    bprm->cred_prepared = 1;
  ...

static void bprm_fill_uid(struct linux_binprm *bprm)
{
    /* Be careful if suid/sgid is set */
    inode_lock(inode);

    /* reload atomically mode/uid/gid now that lock held */
    mode = inode->i_mode;
    uid = inode->i_uid;
    gid = inode->i_gid;
...

在主要方法中,您只需拨打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) {}}); } 即可在成功连接时调用pusher.connect()

subscribeToChannel()
相关问题