[英]How to show custom notification by default with collapsed layout when expand layout is enable in Android
當用戶折疊或展開通知時,我想通過同時支持大小布局來顯示我的自定義通知,如上面的屏幕截圖所示。 但是結果默認情況下顯示擴展的通知。 我想默認將其顯示為折疊通知,並且僅在用戶展開時顯示展開通知。
請檢查下面的代碼:
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_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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.