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