pubnub重复消息android

时间:2015-11-10 09:22:52

标签: android pubnub

我希望有人可以帮助或者至少指出我正确的方向。我正在编写一个发布/接收消息的Android应用程序,我正在尝试使用PubNub来完成此任务。在我看来,我首先取消订阅组名,然后订阅它。当我发布消息时,我成功收到一条消息。如果我然后离开视图并重新输入它,当我发布消息时,我得到一个副本。如果我再次这样做,则消息会重复三次等等。

有人可以查看我的代码或给我任何建议。

非常感谢。

private Pubnub pubnub =  new Pubnub("pub-key", "sub-key");

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(com.rickeshjobanputra.snapapp.R.layout.activity_group);

    GroupName = intent.getStringExtra("GroupName");

    String uuid = "12345";
    pubnub.setUUID(uuid);
    pubnub.unsubscribe(GroupName);

    try {
        pubnub.subscribe(GroupName, new Callback() {
            @Override
            public void successCallback(String channel, final Object message) {
                System.out.println("SUBSCRIBE : " + channel + " : "
                        + message.getClass() + " : " + message.toString());
            }
        });
    } catch (PubnubException e) {
        System.out.println(e.toString());
    }

    addListenerOnButton();
}

private void addListenerOnButton() {
    final Context context = this;
    button = (Button) findViewById(com.rickeshjobanputra.snapapp.R.id.snap_something_button);

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            pubnub.publish(GroupName, "test", new Callback() {
            });
        }
    });
}

更新

    @Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    pubnub.unsubscribeAllChannels();

    try {
        pubnub.subscribe("test", new Callback() {
            @Override
            public void connectCallback(String channel, Object message) {
                pubnub.publish("test", "Hello from the PubNub Java SDK", new Callback() {});
            }

            @Override
            public void successCallback(String channel, final Object message) {
                System.out.println("SUBSCRIBE : " + channel + " : "
                        + message.getClass() + " : " + message.toString());
            }
        });
    } catch (PubnubException e) {
        System.out.println(e.toString());
    }
}

0 个答案:

没有答案