Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

There is my solution:

createNotification(); //your implementationPowerManagerpm= (PowerManager) context.getSystemService(Context.POWER_SERVICE);
booleanisScreenOn= Build.VERSION.SDK_INT >= 20 ? pm.isInteractive() : pm.isScreenOn(); // check if screen is onif (!isScreenOn) {
    PowerManager.WakeLockwl= 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

publicclassFirebaseDataReceiverextendsWakefulBroadcastReceiver {
    
      privatefinalStringTAG="FirebaseDataReceiver";
    
        publicvoidonReceive(Context context, Intent intent) {
            PowerManagerpm= (PowerManager)context.getSystemService(Context.POWER_SERVICE);
            booleanisScreenOn= pm.isScreenOn();
            if(isScreenOn==false)
            {
                PowerManager.WakeLockwl= pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
                wl.acquire(10000);
                PowerManager.WakeLockwl_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 = newIntent(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-permissionandroid: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.

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    PowerManagerpm= (PowerManager) this.getSystemService(Context.POWER_SERVICE);
    booleanisScreenOn= pm.isScreenOn();
    intflags= 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-permissionandroid:name="android.permission.WAKE_LOCK"/>

Post a Comment for "How To Make A Screen Wake Up When A Notification Is Received?"