How to make a screen wake up when a notification is received?
Solution 1
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
Solution 2
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" />
Solution 3
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.
Solution 4
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"/>
Comments
-
Christopher Mckenzie about 2 years
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.
-
shariful islam about 6 yearsBut how I can invoke?
-
The Fluffy Robot over 4 years
SCREEN_DIM_WAKE_LOCK
is deprecated. However, none of the others usingPARTIAL_WAKE_LOCK
have worked for my app. I'm targeting API 19, but using API 28 on my phone -
Velda about 4 years
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 about 4 yearsAlso, 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.
-
ekashking over 3 yearsProblem: ACQUIRE_CAUSES_WAKEUP - when just on its own, it KILLS the Background Service !!!???