Heads-Up Notification - Android Lollipop

Heads-up Notification - Android Lollipop

According to Notifications, you are required to set a vibrate or ringtone to make Heads-up work. However, here's a quick hack that doesn't require VIBRATE permission to produce a head-up notification:

notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);

EDIT:

Don't abuse heads-up notification. See here for when to use heads-up notification:

MAX: For critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.

HIGH: Primarily for important communication, such as messages or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.

Extend Lollipops heads-up notification duration


It worked for me:

  • set ongoing flag to true
  • set full screen intent
  • set priority to PRIORITY_HIGH
  • set category to CATEGORY_CALL

Android notification won't show up as a heads up notification

My bad, when you change channel importance, you need to uninstall the app and install it again. That's why it worked on the OnePlus 6 (because I installed it after changing the notification importance) and not on the Google Pixel 3a (because I was still working with the same install)

Lollipop: heads-up notification is canceled when clicked

For now, I've come up with the following solution:

  1. Add an extra to the contentIntent, indicating it was launched from the notification.
  2. Check for that extra in the launched Activity
  3. If extra is present, repost the notification, but make sure it does not become a heads-up notification.

Code:

@Override
protected void onResume()
{
super.onResume();

if (getIntent().getBooleanExtra("launched_from_notification", false)) {
showNotification(false);
getIntent().putExtra("launched_from_notification", false);
}
}

// If your Activity uses singleTop as launchMode, don't forget this
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
setIntent(intent);
}

private void showNotification(boolean showAsHeadsUp)
{
final Intent intent = getIntent();
intent.putExtra("launched_from_notification", true);

final Notification.Builder nb = new Notification.Builder(this);
nb.setContentTitle("Foobar");
nb.setContentText("I am the content text");
nb.setOngoing(true);
nb.setSmallIcon(android.R.drawable.ic_dialog_info);
nb.setContentIntent(PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
nb.setPriority(Notification.PRIORITY_HIGH);

// Notifications without sound or vibrate will never be heads-up
nb.setDefaults(showAsHeadsUp ? Notification.DEFAULT_ALL : 0);

((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(0, nb.build());
}

Android heads-up notification not showing after disable and enable it on application settings

That is the expected behavior. When the user blocks notifications for a particular notification channel in the notification settings, the importance for that notification channel is reduced. Due to which the notification banner will not show across the top of the device. The user will have to manually set the appropriate settings for the notification channel.

That would be considered as the user's preference and should not be changed forcibly from the application.

If for whatever reason you absolutely need the notification for the proper functioning of the application, you can detect if the user has disabled notifications or if the importance of a notification channel has been changed and ask the user to change it. Check this

If for whatever reason you need to forcibly maintain notification settings, what you can do is to delete notification channels and recreate them with the appropriate importance.

Note: If you delete a notification channel and create a new channel with this same id, the deleted channel will be un-deleted with all of the same settings it had before it was deleted. Thus, you need to create a channel with a new id.

To check if notifications are blocked for the application, use NotificationManager.areNotificationsEnabled(). Documentation

To check if notifications are blocked for a notification channel group, use NotificationChannelGroup.isBlocked(). Documentation

To check if the importance of a notification channel has been changed:

  1. For API 26 to API 28, use NotificationChannel.getImportance() to see if the notification channel importance has been changed. Note that the importance of a channel cannot be changed programmatically once the channel has been created. Documentation
  2. For API 29 and above, use NotificationChannel.hasUserSetImportance(). Documentation

notifications for and above lollipop upto and for nougat auto hide not working

Well, I have solved the issue and I think it would be helpful to someone else having the same issue in showing notifications from a foreground service.


My service class variables declared are

NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
NotificationChannel notificationChannel;
String NOTIFICATION_CHANNEL_ID = "17";

My service class onCreate() is making first notification like

try
{

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this, null);
mBuilder.setContentTitle("Insta Promo")
.setContentText("Get details here..")
.setTicker("Get details here..")
.setSmallIcon(R.drawable.ic_service_success)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{

notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);

// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mNotifyManager.createNotificationChannel(notificationChannel);
}

mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
startForeground(17, mBuilder.build());

}
catch(Exception e)
{
Log.d("xx", "EXCEPTION IN SHOWING NOTIFICATION...\n");
Log.e("xx", "Exception is : ", e);
}

thereafter whenever i need to replace existing notification with another

mBuilder.setContentText("PLEASE ALLOW PERMISSIONS...");
mBuilder.setTicker("PLEASE ALLOW PERMISSIONS...");
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
startForeground(17, mBuilder.build());

If you want to stop foreground service but keep notifications then

stopForeground(false);

and do not call stopself()

Instead from mainactivity OnDestroy Stop this service and clear the notification channel


Hope it helps someone :)



Related Topics



Leave a reply



Submit