Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android heads-up notification not showing

I am trying to make heads-up notification work. Notification is created, but it's not displayed at the top of the app. Here's the code responsible for building a notification:

Notification notification = new NotificationCompat.Builder(context)
                                .setSmallIcon(android.R.drawable.arrow_up_float)
                                .setContentTitle("Check running time - click!")
                                .setContentText(String.valueOf(elapsedTime))
                                .setContentIntent(pendingIntent)
                                .setDefaults(Notification.DEFAULT_ALL)
                                .setPriority(Notification.PRIORITY_HIGH)
                                .setVibrate(new long[0])
                                .build();

The device that I'm trying to run the app is API 21. I've seen many threads, but no solution given works for me.

like image 883
Paweł Poręba Avatar asked Jul 19 '17 18:07

Paweł Poręba


People also ask

Why can't I see my notifications pop up?

Do Not Disturb or Airplane Mode is on. Either system or app notifications are disabled. Power or data settings are preventing apps from retrieving notification alerts. Outdated apps or OS software can cause apps to freeze or crash and not deliver notifications.

Why is my Samsung not showing notifications?

Check Notification Settings Your first course of action is to check the notification settings on your phone to ensure they aren't disabled. Step 1: Launch the Settings app and go to Notifications. Step 2: Tap on Included apps and ensure that switch next to All apps is enabled.


2 Answers

You need to do two things:

  1. Make sure that your notification is properly configured. See: https://developer.android.com/guide/topics/ui/notifiers/notifications#Heads-up

  2. AND make sure the phone is properly configured (I think this is where most get stuck).

Step 1. Configure the notification.

First, register your notification channel like so

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
        NotificationChannel channel = new NotificationChannel("1", name, importance);
        channel.setDescription(description);
        channel.setShowBadge(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

Then, create a notification, like so:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "1")
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle(textTitle)
        .setContentText(textContent)
        .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE) //Important for heads-up notification
        .setPriority(Notification.PRIORITY_MAX); //Important for heads-up notification

Finally, send the notifciations as you would do normally, e.g.:

Notification buildNotification = mBuilder.build();
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(001, buildNotification);

Step 2. Configure the phone.

I noticed that I have to enable some additional settings on my phone (a Xiaomi Note 3):

Here are some ways to reach the menu:

  1. Long press a notification, in the notification bar.
  2. Go to: Settings > Installed apps > Select your app > Notifications
  3. Improving on the previous step, you can help users partially by sending them to the installed apps menu, by using this intent:

startActivity(new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS));

Finally, when you reach this menu enable a setting called something like "Floating notification" (the name of this setting varies between devices).

like image 95
Tom O Avatar answered Oct 19 '22 01:10

Tom O



your code is almost fine. I'm using DEFAULT_VIBRATE instead of DEFAULT_ALL:

builder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) {
   mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
}

But, you can also set

builder.fullScreenIntent(sameAsContentPendingtIntent);

but ^ this one is non-deterministic. I mean that system choose to display a HeadsUp or launch Intent. In most cases it shows HeadUp, but I'm not counting on it because of Android versions, Manufacturers, launchers and so so on.
Not at last, you also need to define right Notification Channel. I think you had already done this, because you wouldn't see any notification if you don't :-) Oh lovely Android :-) Anyway, I want to say that also NotificationChannel needs to be in high priority:

channel = new NotificationChannel("uniqueId", "name", NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);

And also, I advise to you, check latest opinions at developer.android.com
Google is going to be more strict from now. Not only for notifications but also for 'targetApi', but that is another story, pardon me :-)
Have a nice code today

like image 39
zegee29 Avatar answered Oct 19 '22 03:10

zegee29