Full screen intent not starting the activity but do show a notification on android 10
Asked Answered
B

7

17

I try to launch activity for a broadcastReceiver by using the next code

 Intent i = new Intent(context, AlarmNotification.class);
 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // This is at least android 10...

                NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

                if (mgr.getNotificationChannel(CHANNEL_WHATEVER)==null) {
                    mgr.createNotificationChannel(new NotificationChannel(CHANNEL_WHATEVER,
                            "Whatever", NotificationManager.IMPORTANCE_HIGH));
                }

                mgr.notify(NOTIFY_ID, buildNormal(context, i).build());

            }

private NotificationCompat.Builder buildNormal(Context context, Intent intent) {

    NotificationCompat.Builder b=
            new NotificationCompat.Builder(context, CHANNEL_WHATEVER);

    b.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(TEXT)
            .setContentText(TEXT)
            .setFullScreenIntent(buildPendingIntent(context, intent), true);

    return(b);

}

private PendingIntent buildPendingIntent(Context context, Intent intent) {

    return(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}

In the beginning, everything work's perfectly fine. But if I enter the app settings, turn off the notification channel of CHANNEL_WHATEVER, and then turn it on again. Later when I call NotificationManager.notify it shows the notification in the notification drawer but does not start the activity. If I delete the app and reinstall, it works fine again. Is that a bug of android 10 which I should report on, or there is something I can do about it?

Banish answered 10/9, 2019 at 10:11 Comment(5)
After you disable then enable the channel, are all the other options on that channel the same as they were in Settings? Your symptoms match if you are losing IMPORTANCE_HIGH on that channel due to the disable/enable cycle.Wrongdoing
@Wrongdoing How can I tell if I lost the IMPORTANCE_HIGH? And if that's the case, what can I do about it?Banish
"And if that's the case, what can I do about it?" -- nothing, other than perhaps file a bug report. "How can I tell if I lost the IMPORTANCE_HIGH?" -- programmatically, you should be able to look at the result of mgr.getNotificationChannel(CHANNEL_WHATEVER) when it is not null. Call getImportance() and log what value you see.Wrongdoing
@Wrongdoing You right. Importance went from IMPORTANCE_HIGH to IMPORTANCE_LOW. I will report it.Banish
@SimpleUXApps I am also facing the same issue. How did you manage to get it to work? Please post or accept the answer.Barbur
G
27

In Android 10 we need to add permission for USE_FULL_SCREEN_INTENT

Permissions changes for fullscreen intents

  • Apps that target Android 10 or higher and use notifications with fullscreen intents must request the USE_FULL_SCREEN_INTENT permission in their app's manifest file.

  • This is a normal permission, so the system automatically grants it to the requesting app.

Make sure you have added permission in manifest file

SAMPLE CODE

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nilu.demo">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Garfish answered 30/9, 2019 at 4:51 Comment(8)
Thank you for your answer. I am already using this permission.Banish
Hi I am using this permission but not able to show full screen intent? Please helpEqui
Added this permission, users still report that they see only notificationAngellaangelle
@Angellaangelle did you found any solution because I face the same problem now :/Archiepiscopal
@Equi did you found any solution because I face the same problem now :/Archiepiscopal
@KristiyanVarbanov Hey ... Yeah I have solved it but the full screen intent will come on some of the phones only because its operating system dependent ... So you have to add a fallback of heads up notification ... You can try on redmi phones , it will show full screen intentEqui
For my specific needs, I added acquiring locks - PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASEAngellaangelle
@Angellaangelle Where are you acquiring these locks ?Holds
R
4

Android doesn't grant that the activity is shown even if you use a full screen intent:

On some platforms, the system UI may choose to display a heads-up notification, instead of launching this intent, while the user is using the device.

Reduction answered 2/10, 2019 at 11:28 Comment(5)
So how are we supposed to launch an alarm on Android 10? As far as I understand a full-screen intent is the only choice.Banish
you are right it is the only choice but as I said the system can decide to not show the activity, but just show an head-up notificationReduction
when the screen is on, and my app is close then system display a head-up notification, after that, I screen off and see a full-screen activity, why? is this a bug?Kerosene
@FereshtehNaji No. For example, when we receive a call, the call does not hijack the whole screen, if we are using the phone. When the phone is not used, the call takes over the full screen. This is similar.Oppress
I'm also facing similar kind of issue. When you say use Alarm, is it some modification to notification or any other activity? Please provide if any reference for alarm.Buttress
W
2

not sure if this was answered. In addition to Permissions previously mentioned... The only way i found to make this work is to use a "NotificationManager.IMPORTANCE_HIGH" Channel and the Notification. After setting to HIGH, the fullScreenIntent will be opened if the screen is off. Changing the Channel later seems to require an uninstall and reinstall.

Set up the Notification Channel before sending the Notification as (see also):

private static void createNotificationChannel(Context context) {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = **"YOUR CHANNEL"**;
            String description = "YOUR CHANNEL DESCRIPTION";
            int importance = **NotificationManager.IMPORTANCE_HIGH**;
            NotificationChannel channel = new NotificationChannel( "YOUR CHANNEL ID", name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

I set both intents, unclear if you have to set both:

createNotificationChannel((context));
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, **"YOUR CHANNEL**");
    builder.setContentIntent(pendingIntent);
    builder.setFullScreenIntent(pendingIntent,true);
Whangee answered 12/7, 2020 at 14:41 Comment(1)
Thanks; setting the channel's importance to NotificationManager.IMPORTANCE_HIGH is what finally enabled my activity to display full-screen (as triggered by route: AlarmManager.setAlarmClock -> alarm trigger -> NotificationManager.notify), if the screen had been off previously. And yes, I can confirm that if you set the importance to high after the app had already been installed with that channel as "normal" importance previously, then you need to either uninstall->reinstall, or use a new channel id (I took this second route).Indecipherable
T
2

If you want to display your own activity on lock screen you need to enable showOnLockScreen in Manifest

<activity android:name="NewActivity" android:showOnLockScreen="true" /> 

and set flag showWhenLocked in your activity class:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        setShowWhenLocked(true);
        setTurnScreenOn(true);
    }
}

Finally create a fullScreenIntent with your activity class and attach it to notification

Intent fullScreenIntent = new Intent(applicationContext, NewActivity.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(applicationContext, 0,
    fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(applicationContext, channel)
    .setSmallIcon(R.drawable.ic_small)
    .setContentTitle(textTitle)
    .setContentText(textContent)
    .setContentIntent(pendingIntent)
    .setPriority(NotificationCompat.PRIORITY_MAX)
    .setCategory(NotificationCompat.CATEGORY_ALARM)
    .setFullScreenIntent(fullScreenPendingIntent, true)
    .setAutoCancel(true);
Transcalent answered 24/9, 2021 at 11:32 Comment(3)
Adding setShowWhenLocked(true) and setTurnScreenOn(true) in onCreate of the Full screen activity finally did the trick for me.Retard
@Retard Should you add it before or after super.onCreate()?Miscalculate
Shouldn't matter I think.Retard
T
0

You should wake up the screen of Android, before showing the notification:

fun asquireWake() {
    val mPowerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
    val mWakeLock: PowerManager.WakeLock = mPowerManager.newWakeLock(
        PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP,
        "YourApp:Whatever"
    )
    mWakeLock.acquire()
}
Tombac answered 21/1, 2022 at 16:58 Comment(0)
C
0

In my case the problem was in the notification id which is used to show the full screen intent notification.

If we have the previous notification with the same notification id, then the full screen intent activity will not be launched instead heads up notification will be shown in the lock screen.

so try using the unique notification id for the full screen intent notification. For more info about how to setup full screen intent notification:Display time-sensitive notifications

Cherycherye answered 21/3, 2022 at 10:29 Comment(0)
K
-1

for these who has problems with:

builder.setFullScreenIntent(pendingIntent, true)

please try to use instead:

builder.setContentIntent(pendingIntent)

Konya answered 25/3, 2022 at 8:19 Comment(1)
setContentIntent is for tap on notification and setfullscreenintent is feature which automatic open activity without tappingGypsophila

© 2022 - 2024 — McMap. All rights reserved.