For my app I'm trying to get it where the notification wakes up the screen and displays a view from the app. I can't figure how to get the app to wake up when its in a lock screen. I've tried a few things but none seem to work or they crash the app.
-
1https://stackoverflow.com/questions/9631869/light-up-screen-when-notification-received-android – akshay_shahane Jul 31 '17 at 12:22
-
Would https://stackoverflow.com/questions/2891337/turning-on-screen-programmatically help you at all? – Adam Jul 31 '17 at 12:28
4 Answers
There is my solution:
createNotification(); //your implementation
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = Build.VERSION.SDK_INT >= 20 ? pm.isInteractive() : pm.isScreenOn(); // check if screen is on
if (!isScreenOn) {
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "myApp:notificationLock");
wl.acquire(3000); //set your time in milliseconds
}
More at PowerManager

- 496
- 6
- 9
-
1`SCREEN_DIM_WAKE_LOCK` is deprecated. However, none of the others using `PARTIAL_WAKE_LOCK` have worked for my app. I'm targeting API 19, but using API 28 on my phone – The Fluffy Robot Dec 31 '19 at 21:19
-
`ACQUIRE_CAUSES_WAKEUP` is officially recommended way to light up the display to show a notification. From the linked documentation: `Notifications that pop up and want the device to be on are the exception; use this flag to be like them.` – Velda May 04 '20 at 09:33
-
Also, but I'm not sure about this, it's better to use as small amount of time as possible, so Android system won't judge your application for high battery consumption. The device will go into a sleep as usually. – Velda May 04 '20 at 09:54
-
1Problem: ACQUIRE_CAUSES_WAKEUP - when just on its own, it KILLS the Background Service !!!??? – ekashking Oct 18 '20 at 04:16
This BroadCastReceiver
Works For, your App Is in back ground State/ mobile In lock mode. That time when notification is recieved i have to redirect particular screen, for that one i added Intent code,
After receiving Notification this code helps to your requirement
public class FirebaseDataReceiver extends WakefulBroadcastReceiver {
private final String TAG = "FirebaseDataReceiver";
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
if(isScreenOn==false)
{
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
wl.acquire(10000);
PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");
wl_cpu.acquire(10000);
}
//Redirect particular screen after receiving notification, this is like ola driver app concept accepting driver request
intent = new Intent(context, MyTicketListActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
Also remember to specify the permission used in the AndroidManifest.xml
:
<uses-permission android:name="android.permission.WAKE_LOCK" />

- 1,369
- 2
- 6
- 21

- 2,394
- 1
- 19
- 15
Put below code before notification creates,
PowerManager powerManager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "appname::WakeLock");
//acquire will turn on the display
wakeLock.acquire(1*60*1000L);
And make sure to set this permission in the manifest :
<uses-permission android:name="android.permission.WAKE_LOCK"/>

- 2,594
- 20
- 34
I faced a similar situation. It was required to show a notification screen for the user to accept or reject the notification, even when the screen is off. I fumbled with it for a while until now. The requirements demand the screen should be on and this can be achieved with a combination of flags for the window manager and the wake lock as shown below.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
int flags = PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE;
if (!isScreenOn) {
wakeLock = pm.newWakeLock(flags, "my_app:full_lock");
wakeLock.acquire(20000);
}
setContentView(R.layout.activity_incoming_request);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
...
startRingingPhone();
}
This code block will display the activity on the lock screen.

- 812
- 7
- 9