15

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?

Simple UX Apps
  • 611
  • 2
  • 10
  • 20
  • 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. – CommonsWare Sep 29 '19 at 11:00
  • @CommonsWare How can I tell if I lost the IMPORTANCE_HIGH? And if that's the case, what can I do about it? – Simple UX Apps Sep 29 '19 at 12:19
  • 1
    "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. – CommonsWare Sep 29 '19 at 12:22
  • @CommonsWare You right. Importance went from IMPORTANCE_HIGH to IMPORTANCE_LOW. I will report it. – Simple UX Apps Sep 29 '19 at 13:37
  • @SimpleUXApps I am also facing the same issue. How did you manage to get it to work? Please post or accept the answer. – Balasubramanian May 08 '20 at 09:38

8 Answers8

24

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>
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
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.

greywolf82
  • 21,813
  • 18
  • 54
  • 108
  • 1
    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. – Simple UX Apps Oct 05 '19 at 17:40
  • 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 notification – greywolf82 Oct 05 '19 at 18:18
  • 2
    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? – Fereshteh Naji Jan 22 '20 at 05:09
  • @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. – Manish S Jun 10 '21 at 10:09
  • 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. – Ramesh Dec 14 '21 at 10:55
1

Please go through my article on medium on how to launch activity with full screen intent for OS 10. The article also explains how to display headsup notification and to handle action button clicks.

https://medium.com/@dcostalloyd90/show-incoming-voip-call-notification-and-open-activity-for-android-os-10-5aada2d4c1e4

Lloyd Dcosta
  • 289
  • 2
  • 11
  • 1
    Does the full screen intent work if the device is locked ? – Dibzmania Mar 27 '20 at 05:44
  • @Bojke Please refer to my article https://medium.com/@dcostalloyd90/show-incoming-voip-call-notification-and-open-activity-for-android-os-10-5aada2d4c1e4 – Lloyd Dcosta Apr 15 '20 at 10:19
  • 1
    Did not see anything related to notification display on locked screen on your article – Dibzmania Dec 16 '20 at 06:03
  • doesn't work. I have full-screen intent permission and notification exactly how you have it in the article. I receiver a broadcast but the activity doesn't start. – Alex Sep 10 '21 at 18:29
1

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);
user3210655
  • 23
  • 1
  • 5
1

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);
Salcci
  • 11
  • 2
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()
}
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

Rajesh.k
  • 2,327
  • 1
  • 16
  • 19
-1

for these who has problems with:

builder.setFullScreenIntent(pendingIntent, true)

please try to use instead:

builder.setContentIntent(pendingIntent)

Georgy Ivanov
  • 39
  • 1
  • 4
  • setContentIntent is for tap on notification and setfullscreenintent is feature which automatic open activity without tapping – Rahman Rezaee Feb 26 '23 at 16:07