Fullscreen notification when locked and screen is off(not working)
Asked Answered
M

0

0

I want to see a fullscreen activity when I receive a push notification. It works fine when the screen is on/unlocked. But when the screen is off and locked, I dont see the activity. Please help

// Push notification received public class PushNotificationReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
 Intent pushIntent = new Intent(context.getApplicationContext(), CustomNotificationActivity.class);
                    pushIntent.putExtra(CustomNotificationActivity.EXTRA_PUSH_MESSAGE, String.valueOf(pushMessage));
                    pushIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    context.startActivity(pushIntent);
}
}

// Fullscreen activity as notification

 CustomNotificationActivity.java
    public class CustomNotificationActivity extends Activity {
  private KeyguardManager.KeyguardLock lock;
    private PowerManager.WakeLock wakeLock;

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PowerManager pwm = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = pwm.newWakeLock(PowerManager.FULL_WAKE_LOCK, getClass().getSimpleName());
        wakeLock.acquire();
        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        lock = keyguardManager.newKeyguardLock(getClass().getSimpleName());
        lock.disableKeyguard();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
...
...
..

}

   @Override
    public void onPause() {
        super.onPause();
        wakeLock.release();
        lock.reenableKeyguard();
        ......
        finish();
    }
}
Macomber answered 26/7, 2016 at 17:28 Comment(9)
Are you having this problem on all devices or just Samsungs?Discharge
I have only tested in Samsung, will test in other devices n see what happensMacomber
It doesnt seem to work on other devices tooMacomber
Try adding WindowManager.LayoutParams.FLAG_FULLSCREENDischarge
Thanks, the issue was, onpause was called twice.. I was finishing the activity in onPause.. weird y 2 onpauses r getting calledMacomber
@SweetyBertilla your last comment helped me allot. Thankyou.....................Undercroft
sure @Bahu, also try setting the android:launchMode="singleTop" or else there seem to be 2 instances of the activity in backgroundMacomber
Again your comment helped me :-) :-) (and also your ans https://mcmap.net/q/832512/-finish-background-activity-from-broadcast-receiver) thank youUndercroft
@SweetyBertilla, I was struggling since last 3 days.. I read almost all answers of all questions related with lockscreen activity in stackoverflow... but Its your comment that helped me.Eustace

© 2022 - 2024 — McMap. All rights reserved.