在Android中启用展开布局时,如何使用折叠布局默认显示自定义通知

问题描述 投票:0回答:1

我希望通过支持小型和大型布局来显示我的自定义通知,作为用户崩溃或展开通知时的屏幕截图。但结果显示默认情况下扩展通知。我希望默认情况下将其显示为折叠通知,并且仅在用户展开时显示展开的通知。

请检查我的代码:

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

谢谢。

android layout kotlin notifications android-custom-view
1个回答
1
投票

它可能会迟到,但对其他人可能会有所帮助。您可以使用NotificationManager.IMPORTANCE_MIN默认设置折叠通知,并且您可以使用NotificationManager.IMPORTANCE_HIGH.默认设置扩展通知

你可以有完整的例子:

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

© www.soinside.com 2019 - 2024. All rights reserved.