Anael
Anael

Reputation: 470

Android - Show full screen notification when app is in background

I'd like to show a full screen notification when my app is receiving an incoming call. Basically the same as WhatsApp, ie. when the phone received a notification and the app is not started, I want to show a view (activity, notification, whatever you want to call it) that is taking the full size of the screen. Everything is working except one thing: the "view" is never shown in full screen, only as a classic notification.

Here's my code, pretty straightforward. Can someone pinpoint any issue in it? Am I missing some kind of permission or something? Any help would be welcomed.

Intent incomingCallDialog = new Intent(context, IncomingCallActivity.class);
        incomingCallDialog.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        incomingCallDialog.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        if (context.getApplication() != null && ((MyApplication) context.getApplication()).getCurrentActivity() == null) {
            PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0,
                    incomingCallDialog, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(context);
            notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
            notificationBuilder.setContentTitle(context.getString(R.string.incoming_call_from, email));
            notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
            notificationBuilder.setContentText(caller.getName());
            notificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
            notificationBuilder.setCategory(NotificationCompat.CATEGORY_CALL);
            notificationBuilder.setFullScreenIntent(fullScreenPendingIntent, true);
            notificationBuilder.setContentIntent(fullScreenPendingIntent);
            notificationBuilder.setTimeoutAfter(30 * 1000);
            notificationBuilder.setAutoCancel(true);
            notificationBuilder.setWhen(0);

            NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

            String channelId = "my_channel";
            NotificationChannel channel = new NotificationChannel(
                    channelId,
                    "channelname",
                    NotificationManager.IMPORTANCE_HIGH);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            manager.createNotificationChannel(channel);
            notificationBuilder.setChannelId(channelId);

            manager.cancel(NOTIFICATION_CALL_ID);
            manager.notify(NOTIFICATION_CALL_ID, notificationBuilder.build());
        } else {
            context.startActivity(incomingCallDialog);
        }

Upvotes: 2

Views: 747

Answers (1)

Yonathan Ashebir
Yonathan Ashebir

Reputation: 46

Question:
Android - Show full-screen notification when app is in the background


Based on the documentation for setFullScreenIntent (https://developer.android.com/reference/android/app/Notification.Builder#setFullScreenIntent(android.app.PendingIntent,%20boolean)), full-screen intents are intended for urgent, high-priority notifications such as incoming calls or alarms.

From the related article (https://source.android.com/docs/core/permissions/fsi-limits), there are guidelines on how to use this feature and limitations on its behavior, but the exact behavior can depend on the state of the device (locked /unlocked ...) and system-level restrictions.

Key Considerations:

  1. Full-screen intents are designed for notifications that demand the user's immediate attention. Misusing this feature can lead to a poor user experience.
  2. It is crucial to provide users an option to disable full-screen notifications and fall back to regular notifications when they are not essential.
  3. The documentation indicates that there are limitations on when full-screen intents will be shown, depending on factors like device state, but I do not have firsthand experience with how it behaves under specific conditions.

These are untested notes based on research. While this answer is late for the original poster, I hope it is useful to anyone who encounters this issue in the future.

Upvotes: 0

Related Questions