What is the minimum required code to establish a PubNub subscription in a service class? The examples on PubNub include code for on boot subscriptions, broadcast receivers, pushalarms, etc. Am I to believe that all this code from github is the minimum required?
The reason I ask is because I am self-learning code and having a rather rough time implementing services such as PubNub because their documentations are for a level of programmer that I haven't reached yet.
I look at the examples and try to extract just the very basic, bare necessities but I am unsure of what can be stripped from those example classes.
Thank you to someone who understands what I am trying to ask.
EDIT: To be clear this is my current PubNub service class:
public class PubNubService extends Service {
SharedPreferences sP;
static final String pub_key = " - ";
static final String sub_key = " - ";
Pubnub pubnub = new Pubnub(pub_key, sub_key, false);
String channel;
PowerManager.WakeLock wl = null;
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String pnMsg = msg.obj.toString();
final Toast toast = Toast.makeText(getApplicationContext(), pnMsg, Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 200);
}
};
private void notifyUser(Object message) {
Message msg = handler.obtainMessage();
try {
final String obj = (String) message;
msg.obj = obj;
handler.sendMessage(msg);
Log.i("Received msg : ", obj.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public void onCreate() {
super.onCreate();
Toast.makeText(this, "PubnubService created...", Toast.LENGTH_LONG).show();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SubscribeAtBoot");
if (wl != null) {
wl.acquire();
Log.i("PUBNUB", "Partial Wake Lock : " + wl.isHeld());
Toast.makeText(this, "Partial Wake Lock : " + wl.isHeld(), Toast.LENGTH_LONG).show();
}
Log.i("PUBNUB", "PubnubService created...");
try {
pubnub.subscribe(new String[] {channel}, new Callback() {
public void connectCallback(String channel) {
notifyUser("CONNECT on channel:" + channel);
}
public void disconnectCallback(String channel) {
notifyUser("DISCONNECT on channel:" + channel);
}
public void reconnectCallback(String channel) {
notifyUser("RECONNECT on channel:" + channel);
}
@Override
public void successCallback(String channel, Object message) {
notifyUser(channel + " " + message.toString());
}
@Override
public void errorCallback(String channel, Object message) {
notifyUser(channel + " " + message.toString());
}
});
} catch (PubnubException e) {
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (wl != null) {
wl.release();
Log.i("PUBNUB", "Partial Wake Lock : " + wl.isHeld());
Toast.makeText(this, "Partial Wake Lock : " + wl.isHeld(), Toast.LENGTH_LONG).show();
wl = null;
}
Toast.makeText(this, "PubnubService destroyed...", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
This service above is copied from this example. I call to start this service from my MainActivity. I call it like this from my onCreate method:
Intent serviceIntent = new Intent(this, PubNubService.class);
startService(serviceIntent);
The one thing that Android Studio yells at me for is that the Handler class should be static or leaks would occur. When I run my app, the error that occurs is: [Error: 128-0] : Unable to get Response Code. Please contact support with error details. Unable to resolve host "pubsub-1.pubnub.com": No address associated with hostname. And on the next line [Error: 100-1] : Timeout Occurred.
My Android Manifest has this added:
<service android:name=".PubNubService"/>