Android 14自定义通知出现多余边距及RemoteView裁剪问题
Android 14自定义RemoteView通知边距与裁剪问题解决
问题描述
在API<34的Android设备上,基于RemoteView的自定义通知可以完全占据通知区域;但在Android 14(API=34)设备(Pixel 8 Pro模拟器、Tecno Neo 6 Pro 5G)中,自定义通知出现多余边距,RemoteView被裁剪,且无法隐藏应用名称,无错误日志输出。
相关代码
FirebaseMessageReceiver.java
private static RemoteViews contentView; private static NotificationManager notificationManager; private void sendNotification(RemoteMessage remoteMessage) { Intent intent = new Intent(this, MainActivity.class); notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); contentView = new RemoteViews(getPackageName(), R.layout.my_notification_layout); // ... 省略其他逻辑 ... PendingIntent pendingIntent = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { pendingIntent = PendingIntent.getActivity (getApplicationContext(),0,intent, PendingIntent.FLAG_MUTABLE); } else { pendingIntent = PendingIntent.getActivity(getApplicationContext(),0,intent, PendingIntent.FLAG_UPDATE_CURRENT); } // ... 省略其他逻辑 ... String channelId = getString(R.string.default_notification_channel_id); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(iconId) .setPriority(NotificationManager.IMPORTANCE_HIGH) .setDefaults(Notification.DEFAULT_LIGHTS ) .setSound(null) .setVibrate(new long[]{0, 500, 1000, 500, 200, 400, 100, 400, 600}) .setContentIntent(pendingIntent); notificationBuilder.setColor(Color.parseColor("#FF9933")); notificationBuilder.setChannelId(channelId); notificationBuilder.setAutoCancel(false); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { // ... 省略其他逻辑 ... notificationBuilder.setCustomBigContentView(contentView); notificationBuilder.setCustomHeadsUpContentView(contentView); NotificationUpdater.turnOnForeground(this,notifId,notificationManager, notificationBuilder.build()); } else { notificationBuilder.setContent(contentView); notificationBuilder.setOngoing(true); NotificationUpdater.turnOnForeground(this,notifId,notificationManager, notificationBuilder.build()); } }
my_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="560dp" android:background="@drawable/rajasthanibg" android:weightSum="1.0" android:paddingTop="70dp" android:paddingBottom="29dp" android:id="@+id/notification_background"> <ImageView android:id="@+id/imaage" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.355" android:layout_gravity="start|center_horizontal" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.485" android:orientation="vertical" android:gravity="center" android:id="@+id/santsayslayout"> <TextView android:id="@+id/santsays" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textStyle="italic" android:autoSizeTextType="uniform" android:autoSizeMinTextSize="11sp" android:autoSizeMaxTextSize="18sp" android:textSize="15sp" android:gravity="center_horizontal" android:layout_gravity="center_horizontal" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:id="@+id/padlayout" android:layout_weight="0.16"> <TextView android:id="@+id/pad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:autoSizeTextType="uniform" android:autoSizeMinTextSize="11sp" android:autoSizeMaxTextSize="23sp" android:textSize="22sp" android:layout_marginStart="30sp" android:layout_marginEnd="30sp" android:singleLine="false" android:gravity="center_horizontal" android:layout_gravity="center_horizontal" android:textStyle="bold" /> </LinearLayout> </LinearLayout>
解决方案
针对Android 14的通知行为变更,需做以下调整:
1. 适配自定义通知的装饰与布局绑定
Android 14强化了自定义通知的布局约束,需同时设置普通展开态和大视图的自定义布局,并禁用默认装饰元素:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { // 绑定普通展开态的自定义布局 notificationBuilder.setCustomContentView(contentView); notificationBuilder.setCustomBigContentView(contentView); notificationBuilder.setCustomHeadsUpContentView(contentView); // 隐藏应用名称和小图标,移除默认边距 notificationBuilder.setStyle(new NotificationCompat.DecoratedCustomViewStyle() .setShowTitle(false) .setShowIcon(false)); // 保持与低版本一致的Ongoing状态 notificationBuilder.setOngoing(true); NotificationUpdater.turnOnForeground(this,notifId,notificationManager, notificationBuilder.build()); }
2. 调整RemoteView布局的高度约束
原布局固定高度560dp在Android 14中易被裁剪,改为自适应高度,并适配系统最大通知高度:
- 修改布局文件的根布局高度:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content" <!-- 替换固定高度为自适应 --> android:background="@drawable/rajasthanibg" android:weightSum="1.0" android:paddingTop="70dp" android:paddingBottom="29dp" android:id="@+id/notification_background">
- 动态设置最大高度(API34+):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { int maxNotificationHeight = notificationManager.getNotificationMaximumHeight(); contentView.setViewHeight(R.id.notification_background, maxNotificationHeight); }
3. 优化通知渠道配置
确保通知渠道的配置与自定义通知需求匹配:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelId, "自定义通知渠道", NotificationManager.IMPORTANCE_HIGH); channel.setShowBadge(false); channel.setSound(null, null); channel.enableVibration(true); channel.setVibrationPattern(new long[]{0, 500, 1000, 500, 200, 400, 100, 400, 600}); notificationManager.createNotificationChannel(channel); }
内容的提问来源于stack exchange,提问作者Divyansh Luthra
相关产品推荐
相关产品推荐

