• 自定义通知栏显示不全、heads-up通知栏的应用


    首先是自定义显示不全的问题,通常是设置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());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    记得,不同功能的通知栏的channelid和name不要重复,否则会弹不出来,比如常驻和提示通知栏用了一个,因为常驻先出来了,所以提示通知栏就不展示…

    这里是用了32版本的代码,建议as升到Chipmunk以上使用,大家的代码还是要实时更新的呀~
    今天的分享就在这了~嘿嘿

  • 相关阅读:
    你想对构造函数说些什么?
    UDP聊天室项目
    文生图一致性角色生成!谷歌最新文本到图片扩散模型工作
    【leetcode】最少的硬币数目
    加密与安全_探索签名算法
    行业调研方法论, 痛点挖掘方法论
    ReactNative中升级IOS 17版本Crash解决
    论文阅读——InternImage(cvpr2023)
    快速切换目录工具c之windows版本
    L39.linux命令每日一练 -- 第六章 文件备份与压缩命令 -- scp和rsync
  • 原文地址:https://blog.csdn.net/weixin_42405406/article/details/125501596