2

I was hoping someone may be able to help or at least point me in the right direction. I'm writing an android app that publishes/receives messages and i'm trying to use PubNub to accomplish this task. In my view I firstly unsubscribe from a group name and then subscribe to it. When I then publish a message, I successfully get one message. If I then leave the view and re-enter it, when I publish a message I get a duplicate. If I do the same again, the message is triplicated and so on.

Could someone please look at my code or give me any advice.

Many thanks.

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

UPDATE

    @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());
    }
}
Rickesh J
  • 135
  • 6
  • 1
    What is happening in here: ```super.onCreate(savedInstanceState);``` And I suspect maybe this ```addListenerOnButton()``` is getting called each time but not removing the listener from the previous iteration. You might want to have the listener check to see if it is alreayd *listening*. Let me know if that helps. – Craig Conover Nov 10 '15 at 16:03
  • Hi Craig thanks for the response. See [link]http://stackoverflow.com/questions/14671897/super-oncreatesavedinstancestate for why we need `super.onCreate(savedInstanceState)` I removed the listener even though it just handles the onclik event. I updated my code as shown above but the same thing happens. Any other suggestions? – Rickesh J Nov 10 '15 at 20:25
  • 1
    I understand the use of super, I was just wondering if there was any code in the super class that you had implemented or if it is an Android framework class (assume the latter - if so, no worries). But what class are you extending? – Craig Conover Nov 10 '15 at 20:45
  • Is the code in the first box a Fragment or Activity? If its a fragment, are you creating multiple instances of your fragment? – Frederick Brock Nov 10 '15 at 20:57
  • Hi the code is an activity and it extends `AppCompatActivity` – Rickesh J Nov 11 '15 at 09:46
  • 1
    Hi guys, I managed to get it to work! Frederick you were right that I was instantiated a local variable of Pubnub, I moved this declaration to a class that `extends Application` and now it works perfectly. I subscribe to one channel and only one message gets published. Thank you both for your help. It was really appreciated! – Rickesh J Nov 11 '15 at 10:18
  • Beautiful! Fred will update with official answer soon. Please accept it as accepted answer when he does so. Cheers. – Craig Conover Nov 11 '15 at 22:21
  • Can you please help i am facing same problem – Shivam Oberoi Dec 19 '17 at 13:00

0 Answers0