1010import android .os .Handler ;
1111import android .os .Message ;
1212import android .os .PowerManager ;
13+ import android .os .PowerManager .WakeLock ;
1314
1415import com .pubnub .api .Callback ;
1516import com .pubnub .api .Pubnub ;
@@ -46,18 +47,18 @@ public class PubnubService extends Service {
4647 public static String subscribe_key = "pam" ;
4748
4849 public static Pubnub pubnub = null ;
49-
50+
5051 PowerManager .WakeLock wl = null ;
5152
52-
5353 public static void initPubnub () {
54- if (pubnub != null ) return ;
54+ if (pubnub != null )
55+ return ;
5556 pubnub = new Pubnub (publish_key , subscribe_key , false );
5657 pubnub .setUUID (uuid );
5758 pubnub .setAuthKey (auth_key );
5859 pubnub .setMaxRetries (1000 );
5960 }
60-
61+
6162 private void sendNotification (String msg ) {
6263 mNotificationManager = (NotificationManager ) this
6364 .getSystemService (Context .NOTIFICATION_SERVICE );
@@ -69,14 +70,17 @@ private void sendNotification(String msg) {
6970 this ).setSmallIcon (R .drawable .icon )
7071 .setContentTitle ("PubNub Notification" )
7172 .setLights (Color .RED , 3000 , 3000 )
72- .setVibrate (new long [] { 1000 , 1000 , 1000 , 1000 , 1000 } )
73+ .setVibrate (new long [] { 1000 , 1000 , 1000 , 1000 , 1000 })
7374 .setStyle (new NotificationCompat .BigTextStyle ().bigText (msg ))
7475 .setContentText (msg );
7576
7677 mBuilder .setContentIntent (contentIntent );
7778 mBuilder .setAutoCancel (true );
7879 mNotificationManager .notify (NOTIFICATION_ID , mBuilder .build ());
79-
80+
81+ WakeLock screenOn = ((PowerManager )getSystemService (POWER_SERVICE )).newWakeLock (PowerManager .SCREEN_BRIGHT_WAKE_LOCK | PowerManager .ACQUIRE_CAUSES_WAKEUP , "example" );
82+ screenOn .acquire ();
83+
8084 try {
8185 Uri notification = RingtoneManager
8286 .getDefaultUri (RingtoneManager .TYPE_NOTIFICATION );
@@ -86,11 +90,12 @@ private void sendNotification(String msg) {
8690 } catch (Exception e ) {
8791 Log .i ("PUBNUB" , e .toString ());
8892 }
93+ screenOn .release ();
8994 }
90-
95+
9196 private void notifyUser (JSONObject message ) {
9297 Log .i ("PUBNUB" , message .toString ());
93-
98+
9499 String notification = null ;
95100 Object data = null ;
96101 try {
@@ -105,25 +110,24 @@ private void notifyUser(JSONObject message) {
105110 notification = data .toString ();
106111 }
107112
108-
109113 if (PubnubActivity .activityStarted ) {
110114 Intent intent = new Intent ("android.intent.action.MAIN" );
111115 intent .putExtra ("data" , message .toString ());
112116 sendBroadcast (intent );
113117 } else {
114118 sendNotification (notification );
115119 }
116-
120+
117121 if (data .equals ("crash" )) {
118122 // crash now
119123 String s = null ;
120124 System .out .println (s .toString ());
121125 }
122126 }
123-
127+
124128 public int onStartCommand (Intent intent , int flags , int startId ) {
125- super .onStartCommand (intent , flags , startId );
126- return START_STICKY ;
129+ super .onStartCommand (intent , flags , startId );
130+ return START_STICKY ;
127131 }
128132
129133 public void onCreate () {
@@ -145,20 +149,22 @@ public void connectCallback(String channel, Object message) {
145149 try {
146150 pubnub .subscribe (device_channel , new Callback () {
147151 @ Override
148- public void successCallback (String channel , Object message ) {
152+ public void successCallback (String channel ,
153+ Object message ) {
149154 notifyUser ((JSONObject ) message );
150155 }
151156
152157 @ Override
153- public void errorCallback (String channel , Object message ) {
158+ public void errorCallback (String channel ,
159+ Object message ) {
154160 notifyUser ((JSONObject ) message );
155161 }
156162 });
157163 } catch (PubnubException e ) {
158164 Log .i ("PUBNUB" , e .toString ());
159165 }
160166 }
161-
167+
162168 @ Override
163169 public void successCallback (String channel , Object message ) {
164170 notifyUser ((JSONObject ) message );
0 commit comments