44import org .json .JSONException ;
55import org .json .JSONObject ;
66
7+ import android .media .Ringtone ;
8+ import android .media .RingtoneManager ;
9+ import android .net .Uri ;
710import android .os .Handler ;
811import android .os .Message ;
912import android .os .PowerManager ;
@@ -29,53 +32,89 @@ public class PubnubService extends Service {
2932
3033 public static final String ACTION_FROM_SERVICE = null ;
3134
32- String device_channel = "Android-1" ;
33- String public_channel = "public" ;
34- String auth_key = "Android-AuthToken" ;
35- String uuid = "Android-user1" ;
35+ private static final int NOTIFICATION_ID = 1 ;
3636
37- String publish_key = "pam" ;
38- String subscribe_key = "pam" ;
37+ public static String device_channel = "Android-1" ;
38+ public static String public_channel = "public" ;
39+ public static String auth_key = "Android-AuthToken" ;
40+ public static String uuid = "Android-user1" ;
41+ private NotificationManager mNotificationManager ;
3942
40- Pubnub pubnub ;
41- PowerManager . WakeLock wl = null ;
43+ public static String publish_key = "pam" ;
44+ public static String subscribe_key = "pam" ;
4245
43- private void notifyUser (Object message ) {
44- Log .i ("PUBNUB" , message .toString ());
45- Intent intent = new Intent ("android.intent.action.MAIN" );
46- intent .putExtra ("data" , message .toString ());
47- sendBroadcast (intent );
48- }
46+ public static Pubnub pubnub = null ;
47+
48+ PowerManager .WakeLock wl = null ;
4949
50- public void onCreate () {
51- super .onCreate ();
50+
51+ public static void initPubnub () {
52+ if (pubnub != null ) return ;
5253 pubnub = new Pubnub (publish_key , subscribe_key , false );
5354 pubnub .setUUID (uuid );
5455 pubnub .setAuthKey (auth_key );
56+ pubnub .setMaxRetries (1000 );
57+ }
58+
59+ private void sendNotification (String msg ) {
60+ mNotificationManager = (NotificationManager ) this
61+ .getSystemService (Context .NOTIFICATION_SERVICE );
5562
56- pubnub .history (public_channel , 3 , true , 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 = 2 ; i >= 0 ; i --) {
64- notifyUser (jsa .get (i ));
65- }
66- } catch (JSONException e1 ) {
67- // TODO Auto-generated catch block
68- e1 .printStackTrace ();
69- }
63+ PendingIntent contentIntent = PendingIntent .getActivity (this , 0 ,
64+ new Intent (this , PubnubActivity .class ), 0 );
65+
66+ NotificationCompat .Builder mBuilder = new NotificationCompat .Builder (
67+ this ).setSmallIcon (R .drawable .icon )
68+ .setContentTitle ("PubNub Notification" )
69+ .setStyle (new NotificationCompat .BigTextStyle ().bigText (msg ))
70+ .setContentText (msg );
7071
71- }
72+ mBuilder .setContentIntent (contentIntent );
73+ mBuilder .setAutoCancel (true );
74+ mNotificationManager .notify (NOTIFICATION_ID , mBuilder .build ());
7275
73- @ Override
74- public void errorCallback (String channel , PubnubError error ) {
76+ try {
77+ Uri notification = RingtoneManager
78+ .getDefaultUri (RingtoneManager .TYPE_NOTIFICATION );
79+ Ringtone r = RingtoneManager .getRingtone (getApplicationContext (),
80+ notification );
81+ r .play ();
82+ } catch (Exception e ) {
83+ Log .i ("PUBNUB" , e .toString ());
84+ }
85+ }
86+
87+ private void notifyUser (JSONObject message ) {
88+ Log .i ("PUBNUB" , message .toString ());
89+
90+ String notification = null ;
91+ Object data = null ;
92+ try {
93+ data = message .get ("data" );
94+ } catch (JSONException e1 ) {
95+ Log .i ("PUBNUB" , e1 .toString ());
96+ return ;
97+ }
98+ try {
99+ notification = message .getString ("message" );
100+ } catch (JSONException e ) {
101+ notification = data .toString ();
102+ }
75103
76- }
77- });
104+
105+ if (PubnubActivity .activityStarted ) {
106+ Intent intent = new Intent ("android.intent.action.MAIN" );
107+ intent .putExtra ("data" , message .toString ());
108+ sendBroadcast (intent );
109+ } else {
110+ sendNotification (notification );
111+ }
112+ }
113+
78114
115+ public void onCreate () {
116+ super .onCreate ();
117+ initPubnub ();
79118 PowerManager pm = (PowerManager ) getSystemService (Context .POWER_SERVICE );
80119 wl = pm .newWakeLock (PowerManager .PARTIAL_WAKE_LOCK , "SubscribeAtBoot" );
81120 if (wl != null ) {
@@ -86,37 +125,15 @@ public void errorCallback(String channel, PubnubError error) {
86125 Log .i ("PUBNUB" , "PubnubService created..." );
87126
88127 try {
89- pubnub .subscribe (public_channel , new Callback () {
90- @ Override
91- public void connectCallback (String channel , Object response ) {
92- try {
93- pubnub .subscribe (device_channel , new Callback () {
94- @ Override
95- public void successCallback (String channel ,
96- Object message ) {
97- notifyUser (message .toString ());
98- }
99-
100- @ Override
101- public void errorCallback (String channel ,
102- Object message ) {
103- notifyUser (message .toString ());
104- }
105- });
106- } catch (PubnubException e ) {
107- Log .i ("PUBNUB" , e .toString ());
108- }
109-
110- }
111-
128+ pubnub .subscribe (new String []{public_channel , device_channel }, new Callback () {
112129 @ Override
113130 public void successCallback (String channel , Object message ) {
114- notifyUser (message . toString () );
131+ notifyUser (( JSONObject ) message );
115132 }
116133
117134 @ Override
118135 public void errorCallback (String channel , Object message ) {
119- notifyUser (message . toString () );
136+ notifyUser (( JSONObject ) message );
120137 }
121138 });
122139 } catch (PubnubException e ) {
0 commit comments