setFullScreenIntent not working when screen is locked in android 10
Asked Answered
B

3

8

I am trying to set a fullScreentIntent using NotificationCompat in android 10 when the alarm goes on but the fullScreenIntent is not showing even when the alarm goes on.

I added USE_FULL_SCREEN_INTENT in manifest file. I tried acquiring partial wake lock, disabling keyguard but still, the fullscreen intent is not working.

AndroidManifest.xml

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />

AlarmBroadcastReceiver.java

public class AlarmBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "AlarmBroadcastReceiver";
    public static final String STOP_CHANNEL_ID = "Location Alert";
    public static final int ALARM_NOTIFICATION_ID = 2;

    public static MediaPlayer mp;
    public static Vibrator vibrator;

    private boolean isVibrationEnabled = false;
    KeyguardManager keyguardManager;

    @Override
    public void onReceive(Context context, Intent intent) {

        showStopNotification(context);

        long[] mVibratePattern = new long[]{0, 400, 400, 400, 400, 400, 400, 400};
        final int[] mAmplitudes = new int[]{0, 128, 0, 128, 0, 128, 0, 128};

        isVibrationEnabled = intent.getExtras().getBoolean(LocationAlertService.IS_VIBRATE);

        mp = MediaPlayer.create(context, R.raw.ring1);
        mp.setLooping(true);
        mp.start();

        if(isVibrationEnabled) {
            vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                vibrator.vibrate(VibrationEffect.createWaveform(mVibratePattern, mAmplitudes, 0));
            } else {
                //deprecated in API 26
                vibrator.vibrate(mVibratePattern, 3);
            }
        }

    }

    public void showStopNotification(Context context) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            NotificationChannel stopServiceChannel = new NotificationChannel(
                    STOP_CHANNEL_ID,
                    "Location Alert Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(stopServiceChannel);

            Intent wakeupIntent = new Intent(context, WakeUpActivity.class);
            wakeupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            PendingIntent wakeupPendingIntent = PendingIntent.getActivity(context, 0, wakeupIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            Intent mainIntent = new Intent(context, MainActivity.class);
            wakeupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            PendingIntent mainPendingIntent = PendingIntent.getActivity(context, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
            keyguardManager = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);

            PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
            PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
                    | PowerManager.ACQUIRE_CAUSES_WAKEUP
                    | PowerManager.ON_AFTER_RELEASE,
                    "MyApp::" + TAG);
            wakeLock.acquire(10000);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, STOP_CHANNEL_ID)
                    .setSmallIcon(R.drawable.location_alerter)
                    .setContentTitle(context.getResources().getText(R.string.stop_location_alert))
                    .setContentText(context.getResources().getText(R.string.click_to_stop_activity))
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setCategory(NotificationCompat.CATEGORY_CALL)
                    .setFullScreenIntent(wakeupPendingIntent, true);

            Notification alarmNotification = notificationBuilder.build();

            notificationManager.notify(0, alarmNotification);
            wakeLock.release();
        }
        else {
            Intent wakeIntent = new Intent(context, WakeUpActivity.class);
            wakeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(wakeIntent);
        }
    }
}

On onReceive() I am calling showStopNotification() which checks if the device runs on android 10 or not. If it runs on android 10 I am trying to acquire wakelock and launch the activity using NotificationCompat.builder's setFullScreenIntent(). I even set the notification priority as high.

I don't know why the fullScreenIntent is not shown after the alarm goes on. The sound and vibration are working but there is no UI to stop the alarm. Thanks in advance for helping me out.

Botulinus answered 23/12, 2019 at 16:36 Comment(3)
I do have the same problem. Do you found any solution for this?Mandle
No, I did a workaround. When the alarm is triggered I am popping a notification and by clicking that the user can dismiss the alarm. Let me know if you find any other solutions @MandleBotulinus
Could you share your workaround with me? Thanks!Mandle
F
0

Try to improve you notification channel (STOP_CHANNEL_ID) priority to NotificationManager.IMPORTANCE_HIGH

Flaunt answered 25/2, 2020 at 16:19 Comment(0)
I
0

Try adding permission SYSTEM_ALERT_WINDOW as well as USE_FULL_SCREEN_INTENT to your manifest

Interceptor answered 1/2, 2021 at 3:0 Comment(0)
C
0

Do not set Id to notify as 0

notificationManager.notify(0, alarmNotification);

Collar answered 15/3, 2021 at 6:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.