首先是自定义显示不全的问题,通常是设置removeview的方法不对
通过builder.setContent(remoteViews);给通知添加自定义界面,结果由于自定义界面布局过高,底部界面被压缩显示。
用.setCustomBigContentView(mRemoteViews)
setContent 设置普通视图,高度限制为 64 dp
setCustomContentView设置普通视图,高度限制为 64 dp
setCustomBigContentView() 设置扩展视图,高度可以扩展到256dp
setCustomHeadsUpContentView() 设置浮动通知视图
悬浮通知栏的应用(heads-up):
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final NotificationCompat.Builder builder;
builder = new NotificationCompat.Builder(context, HEAD_CHANEL_ID)
.setSmallIcon(R.mipmap.notification_small_icon)
.setContentIntent(pendingIntent)
.setCustomHeadsUpContentView(bigRemoteViews)
.setContent(remoteViews)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setDefaults(android.app.Notification.DEFAULT_ALL)
.setAutoCancel(true)
// Use a full-screen intent only for the highest-priority alerts where you
// have an associated activity that you would like to launch after the user
// interacts with the notification. Also, if your app targets Android 10
// or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
// order for the platform to invoke this notification.
.setFullScreenIntent(pendingIntent, true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(HEAD_CHANEL_ID, HEAD_CHANEL_NAME, NotificationManager.IMPORTANCE_MAX);
notificationManager.createNotificationChannel(channel);
builder.setChannelId(HEAD_CHANEL_ID);
builder.setContentTitle(HEAD_CHANEL_NAME);
}
notificationManager.notify(BIG_INTERCEPTION, builder.build());
记得,不同功能的通知栏的channelid和name不要重复,否则会弹不出来,比如常驻和提示通知栏用了一个,因为常驻先出来了,所以提示通知栏就不展示…
这里是用了32版本的代码,建议as升到Chipmunk以上使用,大家的代码还是要实时更新的呀~
今天的分享就在这了~嘿嘿