How to show heads-up notification on Android 7 and older in Kotlin?

  • Replies:1
  • OpenNot stickiedUnanswered
  • Forum posts: 1

May 21, 2019, 4:42:40 AM via Website

I am creating a messaging app and I need to show heads-up notifications on Android 7 and older up to Android 5.

I have created FirebaseMessagingService together with NotificationChannel to show notifications for Android 8 and higher and it works well with heads-up notifications. On Android 7 FirebaseMessagingService onMessageReceived method doesn't work when the app is in the background. So I've decided to use the BroadcastReceiver to show heads-up notifications and now the heads-up notification is shown on Android 7 for several seconds and then it stays in the drawer together with a normal notification. I even commented out FirebaseMessagingService but still get the ordinary notification together with the heads-up notification. I have a feeling that there is another way of implementing this.


Here is my code:

MyFirebaseMessagingService file:

class MyFirebaseMessagingService : FirebaseMessagingService() {

override fun onMessageReceived(remoteMessage: RemoteMessage) {
//      if (remoteMessage.notification !=null) {
//      showNotification(remoteMessage.notification?.title, remoteMessage.notification?.body)
//      }

fun showNotification(title: String?, body: String?, context: Context) {

        val intent = Intent(context, {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        val pendingIntent = PendingIntent.getActivity(context, 0, intent,
        val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder = NotificationCompat.Builder(context, "my_channel_id_01").apply {
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

Calling the showNotification() from FirebaseBackgroundService file:

class FirebaseBackgroundService : BroadcastReceiver() {

    var myFirebaseMessagingService = MyFirebaseMessagingService()
    var notificationtitle: Any? = ""
    var notificationbody: Any? = ""

    override fun onReceive(context: Context, intent: Intent) {

       if (intent.extras != null) {
            for (key in intent.extras!!.keySet()) {
                val value = intent.extras!!.get(key)
                Log.e("FirebaseDataReceiver", "Key: $key Value: $value")
                if (key.equals("gcm.notification.title", ignoreCase = true) && value != null) {

                    notificationtitle = value

                if (key.equals("gcm.notification.body", ignoreCase = true) && value != null) {

                    notificationbody = value

            myFirebaseMessagingService.showNotification(notificationtitle as String, notificationbody as String, context)

My JSON looks like this:

 "to" : "some-id",

 "notification" : {
     "body" : "Body of Your Notification",
     "title": "Title of Your Notification",
     "content_available" : true,
     "sound": "sound1.mp3",

     "click_action" : "chat"
"data": {
     "uid"  : "yOMX4OagvgXEl4w4l78F7SlqzKr2",
     "method" : "chat",
     "android_channel_id": "1"
  • Forum posts: 23

May 21, 2019, 2:40:57 PM via Website

How to Get Heads Up notifications in Android

Android Lollipop brings lots of good things for devices. One of the most useful things is to make the design attractive with animation by material design, along with that it also introduces a new type of notification which is known as “Heads-up notification.

When the device gets a high-priority notification, it thinks like as floating window notifications that appear at the top of the screen and will be presented to the user for short period of time with an expanded layout and exposing possible actions. You can either tap them to open them or swipe them away to dismiss them. This type of notification appears only when the device is active (that is, the device is unlocked and its screen is on).

Heads-up Notification will alerts user on the incoming phone call, alarm, new message, low battery, calendar-based events etc.

If Notification’s priority is flagged as High, Max, or full-screen, it gets a Heads-up notification. User can also configure priority modes from

From here User can set all type of setting for Notification.

Here is an example code of Heads-up notification :

//build notification
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setContentTitle("Ping Notification")
.setContentText("Tomorrow will be your birthday.")
.setDefaults(Notification.DEFAULT_ALL) // must requires VIBRATE permission
.setPriority(NotificationCompat.PRIORITY_HIGH) //must give priority to High, Max which will considered as heads-up notification
getString(R.string.dismiss), piDismiss)
getString(R.string.snooze), piSnooze);

//set intents and pending intents to call service on click of "dismiss" action button of notification
Intent dismissIntent = new Intent(this, MyService.class);
PendingIntent piDismiss = PendingIntent.getService(this, 0, dismissIntent, 0);

//set intents and pending intents to call service on click of "snooze" action button of notification
Intent snoozeIntent = new Intent(this, MyService.class);
PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);

// Gets an instance of the NotificationManager service
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//to post your notification to the notification bar with a id. If a notification with same id already exists, it will get replaced with updated information.

If you are in the middle of doing something else on your device and don’t want to deal with an incoming “heads-up” notification right away but you want to keep it around so you’ll remember to deal with it later but you have only choice to stop what you are doing and wait about 10 seconds until the card disappears, at which point it’ll move up into your notification panel as a regular alert. If you swipe the card away, the notification will get dismissed.