Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a screen wake up when a notification is received?

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.

like image 445
Christopher Mckenzie Avatar asked Jul 31 '17 12:07

Christopher Mckenzie


2 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

like image 152
Jan Moravec Avatar answered Nov 15 '22 15:11

Jan Moravec


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"/>
like image 38
Hardik Hirpara Avatar answered Nov 15 '22 16:11

Hardik Hirpara