2

I am developing an Android app with FCM notification. When the app is either in background or foreground, I receive the notification properly; it sounds & vibrates, clicking on the notification launches the application. Everything is proper. But when the device is locked and display is off, the device vibrates and makes sound upon notification reception. But it does not wake up (screen does not turn ON). Only after pressing the keys the device wakes up and I could see the notification even in the locked screen.

I have two questions,

  1. How to turn the display ON ?

  2. How to show the application icon in the locked time screen, just like whatsapp logo in the locked screen?

Thanks, Velu

Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
Velu
  • 53
  • 1
  • 6

1 Answers1

6

You can create a method in which you verify if the screen is on or off:

private static void wakeUpScreen() {
  // Wake up screen
  PowerManager powerManager = (PowerManager)BuyersApp.getContext().getSystemService(Context.POWER_SERVICE);
  boolean isScreenOn;
  if (Build.VERSION.SDK_INT >= 20) {
    isScreenOn = powerManager.isInteractive();
  } else {
    isScreenOn = powerManager.isScreenOn();
  }
  if (!isScreenOn){
    PowerManager.WakeLock wl = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MH24_SCREENLOCK");
    wl.acquire(2000);
    PowerManager.WakeLock wl_cpu = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MH24_SCREENLOCK");
    wl_cpu.acquire(2000);
  }
}

Also remember to add the permissions in manifest:

<uses-permission android:name="android.permission.WAKE_LOCK" />

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

check this answer

Carlos Daniel
  • 2,459
  • 25
  • 30