在Android中启用展开布局时,默认情况下如何显示具有折叠布局的自定义通知 [英] How to show custom notification by default with collapsed layout when expand layout is enable in Android

查看:826
本文介绍了在Android中启用展开布局时,默认情况下如何显示具有折叠布局的自定义通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户折叠或展开通知时,我想通过同时支持大小布局来显示我的自定义通知,如上面的屏幕截图所示.但是结果默认情况下显示扩展的通知.我想默认将其显示为折叠的通知,并且仅在用户展开时显示展开的通知.

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)
}

谢谢.

推荐答案

可能会晚一些,但可能对其他人有帮助.您可以默认使用 NotificationManager.IMPORTANCE_MIN 设置折叠的通知,默认情况下可以使用 NotificationManager.IMPORTANCE_HIGH.

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;
    }

这篇关于在Android中启用展开布局时,默认情况下如何显示具有折叠布局的自定义通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆