Question

I'm using in my app the PubNub SDK for Android. I try to subscribe to a channel but nothing happens.

here is my code for connecting to pubnub:

public class MyPubnub {
    private static Pubnub pubnub = null;
    private static final String publishKey = "MY PUBLISH KEY";
    private static final String subscribeKey = "MY SUBSCRIBER KEY";
    private static final String secretKey = "MY SECRET KEY";

    private MyPubnub() {
    }

    public static Pubnub getPubnubObject() {
        if (pubnub == null) {
            pubnub = new Pubnub(publishKey, subscribeKey, secretKey, null,
                    false);
        }
        return pubnub;
    }
}


code for subscribing to pubnub:

public void onCreate(Bundle b) {
    super.onCreate(b);
    initUI();
    Pubnub pn = MyPubnub.getPubnubObject();
    String channel = getChannelName();
    Reciver reciver = new Reciver();

    pn.subscribe(channel, reciver);
}
class Reciver implements Callback {

    @Override
    public boolean execute(Object message) {
        Log.d("PUBNUB", "message received [" + message.toString() + "]");
        return true;
    }
}


in the debugging process I noticed that the app freezes once I try to run this line: pn.subscribe(channel, reciver); (nothing happens, no response no nothing).

any thoughts?

Was it helpful?

Solution

PubNub Android SDK

You are asking about the PubNub Android API using the PubNub Subscribe function to receive remote messages on the Android Mobile Phone Device. Regarding the singleton pattern you are using, this looks fine! :-)

PubNub Android SDK Documentation: http://www.pubnub.com/docs/java/android/android-sdk.html

However it sounds like you are being blocked on the Subscribe call. Are you using the most recent PubNub Android API - https://github.com/pubnub/java/tree/master/android - The latest API supposed to allow you to automatically call the subscribe API without blocking your app. However if this is the case, and the subscribe() method is indeed blocking your app, you can solve this by placing this function call inside a Java Thread and sending message posts to your parent main program when a new message arrives.

So the final solution will be to place the PubNub method calls inside a new Thread, sending updates to the parent on receiving an update from the callback:

https://github.com/pubnub/java/tree/master/android

Thread t = new Thread() {
    public void run() {
        HashMap<String, Object> args = new HashMap<String, Object>(6);
        args.put("channel", _channel);

        // callback to get response
        args.put( "callback", new ReceivedMessage() );

        // Listen for Messages (Subscribe)
        _pubnub.subscribe(args);
    };
};
t.start();
threads.put(_channel, t);

Then you implement your ReceiveMessage() class: https://github.com/pubnub/java/tree/master/android - see the source code.

Note that this isn't a full copy/paste answer, however it will get you pointed in the right direction. Plus this Threading style will be backwards compatible with future upgrades.

So in the end, using Threads() is your answer here.

OTHER TIPS

Don't pass null on cypherkey, just pass "". I was getting an null exception and not firing the message arrival event.

Replace this: pubnub = new Pubnub(publishKey, subscribeKey, secretKey, null, false);

By this: pubnub = new Pubnub(publishKey, subscribeKey, secretKey, "", false);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top