Search code examples
androidandroid-intentandroid-activityandroid-notifications

Android notification setFullScreenIntent doesn't automatically launch the activity


I want to start a fullscreen activity automatically from a notification but I have trouble with the setFullScreenIntent. I followed this : https://developer.android.com/training/notify-user/time-sensitive and threads on internet but it's not workking for me. My notification appears but I have to press it to launch the activity.

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = this.getSystemService(NotificationManager.class);

            if (notificationManager != null && notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID) == null) {
                NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "channel_name", NotificationManager.IMPORTANCE_HIGH);
                channel.setDescription("channel_description");
                notificationManager.createNotificationChannel(channel);
            }            
        }

        Intent fullScreenIntent = new Intent(this, ResultActivity.class);
        fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
                fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                        .setSmallIcon(R.drawable.notification_icon)
                        .setContentTitle("Full Screen Notification")
                        .setContentText("Notification test text")
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setCategory(NotificationCompat.CATEGORY_CALL)
                        .setDefaults(NotificationCompat.DEFAULT_ALL)
                        .setFullScreenIntent(fullScreenPendingIntent, true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(12345, notificationBuilder.build()); 

Here my manifest :

  <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
  <activity
        android:name=".ResultActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" /> -->
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
  </activity>

Thank you !


Solution

  • I am guessing that when you lock the phone, the Full-Screen Intent is shown properly, right?

    What you are seeing is called heads-up notifications. As stated at the documentation, it's the normal behavior for high importance notifications and they appear when the device is unlocked.

    What should you do?

    Keep the full-screen intent for locked screens but also provide a custom layout to your notification which will present your feature in a better, contextual UI rather than the default notification.


    Example:

    The alarm application (on Pixel phones) has the following cases for triggered alarms:

    • Launches a heads-up notification when the screen is unlocked
    • Full-screen intent when the screen is locked.