4

I want to show my custom notification by support both small and large layout as screenshots above when user collapse or expand the notification. But the result it shows expanded notification by default. I want to show it as collapsed notification by default and only show expanded notification when user expand it.

Please check my code bellow:

 private fun initCustomNotification() {
    // Get the layouts to use in the custom notification
    val notificationLayout = RemoteViews(packageName, R.layout.custom_notification_small_layout)
    val notificationLayoutExpanded = RemoteViews(packageName, R.layout.custom_notification_large_layout)

    // Apply the layouts to the notification
    customNotificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.dog)
            .setStyle(NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(notificationLayout)
            .setCustomBigContentView(notificationLayoutExpanded)
            .setOngoing(true)
            .setShowWhen(false)
}

Thanks.

Chivorn
  • 2,203
  • 2
  • 21
  • 37

1 Answers1

9

It might be late but it may be helpful for others. You can set collapsed notification by default with NotificationManager.IMPORTANCE_MIN and you can set expanded notification by default with NotificationManager.IMPORTANCE_HIGH.

You can have full example:

public void generateCollepedNotification(Context context, String notificationTitle, String notificationSubText, String notificationMessage) {
        String channelId = "my_channel_id";
        int notification_id = 1001;

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
                .setSmallIcon(R.mipmap.ic_logo_notification)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_logo)) // optional
                .setContentTitle(notificationTitle)
                .setContentText(notificationMessage)
                .setSubText(notificationSubText) // optional
                .setColor(ContextCompat.getColor(context, R.color.colorPrimary)) // optional
                .setAutoCancel(true);

        getNotificationManagerIMPORTANCE_MIN(context, channelId).notify(notification_id, builder.build());
    }



private NotificationManager getNotificationManagerIMPORTANCE_MIN(Context context, String channelId) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            String channelName = "My Channel Name";
            String channelDescription = "This is Description of my channel";
            NotificationChannel mChannel = new NotificationChannel(
                    channelId,
                    channelName,
                    NotificationManager.IMPORTANCE_MIN
            );
            mChannel.setDescription(channelDescription);
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            mChannel.setShowBadge(false);
            notificationManager.createNotificationChannel(mChannel);
        }
        return notificationManager;
    }

enter image description here

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
  • Thank bro. I will try it later when I'm free. – Chivorn Apr 19 '19 at 06:47
  • Okay Chivorn, let me know if you have any query regarding it. – Rumit Patel Apr 19 '19 at 06:48
  • Is there a way to only show it expanded? My users complain because "sometimes" it is not show correctly (because it is shown collapsed). So I would like to force it to be always expanded. Is it possible? – Ton Jul 09 '20 at 18:43
  • Its working. But i am facing some issues for startForeground serivice. Its crashing after few second. because "startForeground(notificationId, builder.build() )" must call. if i call it then it's not working and showing expended notification. Can you please help me. – Vinod Prasad V-9-द Feb 19 '21 at 23:36
  • @Ton It's already mentioned in the answer. "you can set expanded notification by default with `NotificationManager.IMPORTANCE_HIGH.` " – Rumit Patel Jul 27 '21 at 14:17