What does the absolute minimum PubNub service code look like?

What is the minimum code needed to install a PubNub subscription in a service class? PubNub examples include code for boot subscriptions, broadcast receivers, pushalarms, etc. I suppose all this code from github is the minimum required?

The reason I ask is because I am a self-learning code and pretty rudely performing services like PubNub because their documentation is for a programmer level that I haven't reached yet.

I look at examples and try to extract only the simplest, necessary things, but I'm not sure what can be removed from these classes.

Thanks to someone who understands what I'm trying to ask.

EDIT: To be clear, this is my current PubNub class of service:

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

The service above is copied from this example. I am calling to start this service with my MainActivity. I call it this from my onCreate method:

Intent serviceIntent = new Intent(this, PubNubService.class);
    startService(serviceIntent);

One thing that Android Studio yells at me is that the Handler class must be static or leak. When I launch my application, an error occurs: [Error: 128-0]: Unable to get response code. Contact support with error information. Failed to resolve host "pubsub-1.pubnub.com": there is no address associated with the host name . And on the next line [Error: 100-1]: timeout .

In my Android manifest, added:

<service android:name=".PubNubService"/>
+4
1

PubNub Android

. PKNub Android SDK.

import com.pubnub.api.*;
import org.json.*;


Pubnub pubnub = new Pubnub("your_pub_key", "your_sub_key");

pubnub.subscribe("channel1", new Callback() {
      @Override
      public void connectCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : CONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      @Override
      public void disconnectCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : DISCONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      public void reconnectCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : RECONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      @Override
      public void successCallback(String channel, Object message) {
          System.out.println("SUBSCRIBE : " + channel + " : "
                     + message.getClass() + " : " + message.toString());
          // this is the messages received from publish
          // add these messages to a list UI component
      }

      @Override
      public void errorCallback(String channel, PubnubError error) {
          System.out.println("SUBSCRIBE : ERROR on channel " + channel
                     + " : " + error.toString());
      }
    }
  );

= Callback() { public void successCallback (String channel, Object response) {   System.out.println(response.toString()); } public void errorCallback ( String, PubnubError) {   System.out.println(Error.toString()); } }; pubnub.publish( "my_channel", "Hello PubNub Java SDK!", );

, . -, , . successCallback , .

.

, , .

, Subscribe in Boot , (), . , . . SO Android , .

​​ Android

SO " pubnub

0

Source: https://habr.com/ru/post/1625937/


All Articles