In my Android Application i don’t have a problem how to use notification in service. But i don’t know how to show my notification in upper side of display and always on top of all activites when it’s comming. For example, it happens when an incoming SMS message is received, the part of the status bar that is associated with the notification is temporarily shown in the upper part of the screen. enter image description here
Advertisement
Answer
HOW TO MAKE A HEADS-UP NOTIFICATION
For that we need a NotificationCompat.Builder()
that requires a channel ID from a NotificationChannel
.
The notification will only appears as a heads-up notification if the NotificationChannel
is created with an importance value of NotificationManager.IMPORTANCE_HIGH
:
NotificationChannel channel = new NotificationChannel("channel01", "name", NotificationManager.IMPORTANCE_HIGH); // for heads-up notifications channel.setDescription("description"); // Register channel with system NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel);
Use this snippet to show the heads-up notification:
Notification notification = new NotificationCompat.Builder(this, "channel01") .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle("Test") .setContentText("You see me!") .setDefaults(Notification.DEFAULT_ALL) .setPriority(NotificationCompat.PRIORITY_HIGH) // heads-up .build(); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(0, notification);
In addition I found a useful Tutorial that you can follow along. It’s sourcecode is provided in github as well.
Result: Heads-up notification