Skip to content

Commit 4802ade

Browse files
author
Devendra
committed
wake up screen on message
1 parent eff30c3 commit 4802ade

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

‎android/examples/PubnubSubscribe/src/com/pubnub/examples/pubnubsubscribe/PubnubActivity.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import android.media.RingtoneManager;
2424
import android.net.Uri;
2525
import android.os.Bundle;
26+
import android.os.PowerManager;
27+
import android.os.PowerManager.WakeLock;
2628
import android.support.v4.app.NotificationCompat;
2729
import android.util.Log;
2830
import android.view.View;
@@ -61,6 +63,9 @@ private void sendNotification(String msg) {
6163
mBuilder.setAutoCancel(true);
6264
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
6365

66+
WakeLock screenOn = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "example");
67+
screenOn.acquire();
68+
6469
try {
6570
Uri notification = RingtoneManager
6671
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
@@ -70,6 +75,7 @@ private void sendNotification(String msg) {
7075
} catch (Exception e) {
7176
Log.i("PUBNUB", e.toString());
7277
}
78+
screenOn.release();
7379
}
7480

7581
public void notifyUser(JSONObject message) {

‎android/examples/PubnubSubscribe/src/com/pubnub/examples/pubnubsubscribe/PubnubService.java‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.os.Handler;
1111
import android.os.Message;
1212
import android.os.PowerManager;
13+
import android.os.PowerManager.WakeLock;
1314

1415
import com.pubnub.api.Callback;
1516
import 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

Comments
 (0)