11package com .pubnub .examples .pubnubsubscribe ;
22
33
4+ import org .json .JSONArray ;
5+ import org .json .JSONException ;
6+
47import android .os .Handler ;
58import android .os .Message ;
69import android .os .PowerManager ;
710
811import com .pubnub .api .Callback ;
912import com .pubnub .api .Pubnub ;
13+ import com .pubnub .api .PubnubError ;
1014import com .pubnub .api .PubnubException ;
1115
1216
17+ import android .app .NotificationManager ;
18+ import android .app .PendingIntent ;
1319import android .app .Service ;
1420import android .content .Context ;
1521import android .content .Intent ;
1622import android .os .IBinder ;
23+ import android .support .v4 .app .NotificationCompat ;
1724import android .util .Log ;
1825import android .widget .Toast ;
1926
2027public class PubnubService extends Service {
2128
29+ private static final String TAG = "PubnubGcm" ;
30+
2231 public static final String ACTION_FROM_SERVICE = null ;
23- String channel = "hello_world1" ;
24- Pubnub pubnub = new Pubnub ("demo" , "demo" , false );
32+
33+ String device_channel = "Android-1" ;
34+ String public_channel = "public" ;
35+ String auth_key = "Android-AuthToken" ;
36+ String uuid = "Android-user1" ;
37+
38+ String publish_key = "pam" ;
39+ String subscribe_key = "pam" ;
40+
41+ Pubnub pubnub ;
2542 PowerManager .WakeLock wl = null ;
2643
27- private final Handler handler = new Handler () {
28- public void handleMessage (Message msg ) {
29- String pnMsg = msg .obj .toString ();
30-
31- final Toast toast = Toast .makeText (getApplicationContext (), pnMsg , Toast .LENGTH_SHORT );
32- toast .show ();
33-
34- Handler handler = new Handler ();
35- handler .postDelayed (new Runnable () {
36- @ Override
37- public void run () {
38- toast .cancel ();
39- }
40- }, 200 );
41-
42- }
43- };
44-
4544 private void notifyUser (Object message ) {
46-
47- Message msg = handler .obtainMessage ();
48-
4945 Intent intent = new Intent ("android.intent.action.MAIN" );
5046 intent .putExtra ("data" , message .toString ());
5147 sendBroadcast (intent );
52- /*
53- try {
54- final String obj = (String) message;
55- msg.obj = obj;
56- handler.sendMessage(msg);
57- Log.i("Received msg : ", obj.toString());
58-
59- } catch (Exception e) {
60- e.printStackTrace();
61- }
62- */
6348 }
6449
6550 public void onCreate () {
6651 super .onCreate ();
67- //Toast.makeText(this, "PubnubService created...", Toast.LENGTH_LONG).show();
52+ pubnub = new Pubnub (publish_key , subscribe_key , false );
53+ pubnub .setUUID (uuid );
54+ pubnub .setAuthKey (auth_key );
55+
56+ pubnub .history (public_channel , 3 , new Callback (){
57+ @ Override
58+ public void successCallback (String channel , Object message ) {
59+ Log .i ("PUBNUB" , message .toString ());
60+ JSONArray jsa ;
61+ try {
62+ jsa = (JSONArray ) ((JSONArray ) message ).get (0 );
63+ for (int i =0 ; i < 3 ; i ++) {
64+ notifyUser (jsa .get (0 ));
65+ }
66+ } catch (JSONException e1 ) {
67+ // TODO Auto-generated catch block
68+ e1 .printStackTrace ();
69+ }
70+
71+
72+
73+ }
74+ @ Override
75+ public void errorCallback (String channel , PubnubError error ) {
76+
77+ }
78+ });
79+
6880 PowerManager pm = (PowerManager ) getSystemService (Context .POWER_SERVICE );
6981 wl = pm .newWakeLock (PowerManager .PARTIAL_WAKE_LOCK , "SubscribeAtBoot" );
7082 if (wl != null ) {
7183 wl .acquire ();
7284 Log .i ("PUBNUB" , "Partial Wake Lock : " + wl .isHeld ());
73- //Toast.makeText(this, "Partial Wake Lock : " + wl.isHeld(), Toast.LENGTH_LONG).show();
7485 }
7586
7687 Log .i ("PUBNUB" , "PubnubService created..." );
88+
89+
90+
7791 try {
78- pubnub .subscribe (new String [] { channel } , new Callback () {
92+ pubnub .subscribe (public_channel , new Callback () {
7993 public void connectCallback (String channel ) {
80- //notifyUser("CONNECT on channel:" + channel);
94+
95+ try {
96+ pubnub .subscribe (device_channel , new Callback () {
97+ public void connectCallback (String channel ) {
98+ //notifyUser("CONNECT on channel:" + channel);
99+ }
100+ public void disconnectCallback (String channel ) {
101+ //notifyUser("DISCONNECT on channel:" + channel);
102+ }
103+ public void reconnectCallback (String channel ) {
104+ //notifyUser("RECONNECT on channel:" + channel);
105+ }
106+ @ Override
107+ public void successCallback (String channel , Object message ) {
108+ notifyUser (message .toString ());
109+ }
110+ @ Override
111+ public void errorCallback (String channel , Object message ) {
112+ notifyUser (message .toString ());
113+ }
114+ });
115+ } catch (PubnubException e ) {
116+ // TODO Auto-generated catch block
117+ e .printStackTrace ();
118+ }
119+
81120 }
82121 public void disconnectCallback (String channel ) {
83122 //notifyUser("DISCONNECT on channel:" + channel);
@@ -97,7 +136,10 @@ public void errorCallback(String channel, Object message) {
97136 } catch (PubnubException e ) {
98137
99138 }
139+
100140 }
141+
142+
101143
102144 @ Override
103145 public void onDestroy () {
0 commit comments