Durian Duraman
Durian Duraman

Reputation: 31

Android, Java - setSmallIcon() hidden on new notification. But the notification still there in the notification drawer

I am really lost here. Is there any scenario where a notification icon in statusbar is hidden but still the notification is present in drawer? This happens when appending incoming messages to the notification after replying to the already existing notification.

I use :

NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification();

Everything works fine but the notification icon goes to hiding. I also use

NotificationManager.IMPORTANCE_HIGH

Here is the notification builder code.

   NotificationCompat.MessagingStyle activeNotification = NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification();
  
                    return new NotificationCompat.Builder(getApplicationContext(), ID)
                            .setContentIntent(pIntent)
                           .addAction(action)
                            .setSmallIcon( R.drawable.icon)
                            .setAutoCancel(true)
                              .setSound(uri)
                             .setStyle(activeNotification
                                    .addMessage(body, SystemClock.currentThreadTimeMillis(), person)
                              )
                            ;

Thanks in advance.

Upvotes: 1

Views: 703

Answers (2)

Durian Duraman
Durian Duraman

Reputation: 31

Looks like the issue arrives when you pass a title different than the last appended one to the notification for next appending. This somehow removes the smallIcon. So i just supplied null as title for incoming new replies for appending and it worked fine. Still i need to dig deep to re-write the code to fit appropriately.

Upvotes: 0

Monim Kaiser
Monim Kaiser

Reputation: 65

You can follow below Notification Builder. I am setting small icons like this:

val notificationBuilder: NotificationCompat.Builder =
        NotificationCompat.Builder(context, channelId)

notificationBuilder.setSmallIcon(R.mipmap.ic_launcher_round)

UPDATE:

I am posting my full code for showing notification.

val pendingIntent =
        PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

    val notificationBuilder: NotificationCompat.Builder =
        NotificationCompat.Builder(context, channelId)

    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher_round)
    notificationBuilder.color = 0x474E54

    notificationBuilder.setContentTitle(title)
        .setContentText(message)
        .setAutoCancel(false)
        .setStyle(NotificationCompat.BigTextStyle())
        .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
        .setContentIntent(pendingIntent)
        .setDefaults(Notification.DEFAULT_ALL)
        .setAutoCancel(true)
        .setCategory(NotificationCompat.CATEGORY_SOCIAL)

    val notificationManager: NotificationManager =
        context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val importance = NotificationManager.IMPORTANCE_HIGH
        val notificationChannel = NotificationChannel(
            channelId,
            channelId,
            importance
        )

        notificationChannel.enableLights(true)
        notificationChannel.lightColor = Color.RED
        notificationChannel.enableVibration(true)
        notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)
        notificationBuilder.setChannelId(channelId)
        notificationManager.createNotificationChannel(notificationChannel)

    } else {
        notificationBuilder.priority = NotificationCompat.PRIORITY_HIGH
        notificationBuilder.setVibrate(longArrayOf(0, 1000, 500, 1000))
    }

    notificationManager.notify(notificationId, notificationBuilder.build())

You can change it as per you need. Hope this will help. :)

Upvotes: 1

Related Questions