Christopher Mckenzie
Christopher Mckenzie

Reputation: 275

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.

Upvotes: 14

Views: 20816

Answers (4)

Hardik Hirpara
Hardik Hirpara

Reputation: 3046

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"/>

Upvotes: 2

Kishore Reddy
Kishore Reddy

Reputation: 2454

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" />

Upvotes: 5

Clement Osei Tano
Clement Osei Tano

Reputation: 832

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.

Upvotes: 1

Jan Moravec
Jan Moravec

Reputation: 536

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

Upvotes: 17

Related Questions