Android Notifaction 从4.0到7.0

2023-11-16

Android Notifaction 从4.0到7.0

图1 4.0 通知样式

4.4.4

图2 5.0 6.0 通知样式

5.0 6.0

图3 7.0 通知样式

7

兼容

这么多版本,如何兼容,没关系,交给
android.support.v7.app.NotificationCompat

基本用法

    NotificationManager nm = (NotificationManager) ctx.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

通过builder设置notifaction的属性

具体方法见:官方API,国内可用

高级用法

就是这些style

NotificationCompat.BigPictureStyle

NotificationCompat.BigTextStyle

NotificationCompat.InboxStyle

NotificationCompat.MediaStyle

NotificationCompat.DecoratedCustomViewStyle

NotificationCompat.MessagingStyle

小技巧

横幅

两种方式

1.builder.setPriority(Notification.PRIORITY_MAX);

2.builder.setFullScreenIntent(intent,false);

通知无声音、无震动、无LED呼吸灯闪烁

        long[] pattern = {0,0};
        builder.setVibrate(pattern);
        builder.setLights(Color.rgb(0,0,0),0,0);
        builder.setSound(null);

异步下载通知里显示的图片

如果你是用glide图片加载框架,那么恭喜你中奖了,今天我介绍的就是glide,同样的Picasso也有类似的方法,用法基本差不多,就不多介绍了
常用的方法要不就是本地图片,要不就是先下载再显示通知,这里我们也说两种

1.直接显示,异步加载通知区域图片

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // Inflate and set the layout for the expanded notification view
        RemoteViews expandedView = new RemoteViews(ctx.getPackageName(), R.layout.notification_expanded);
        expandedView.addView(R.id.content_rl, notification.contentView.clone());
        notification.bigContentView = expandedView;
        NotificationTarget notificationTarget = new NotificationTarget(context.getApplicationContext(), expandedView, R.id.expanded_imageView, notification, id);
        Glide.with(context.getApplicationContext()) // safer!
                .load(bigPicture)
                .asBitmap()
                .into(notificationTarget);
    }

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right|top"
android:orientation="vertical">

<RelativeLayout
    android:id="@+id/content_rl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></RelativeLayout>


<ImageView
    android:id="@+id/expanded_imageView"
    android:layout_width="match_parent"
    android:layout_height="152dp"
    android:contentDescription="@string/app_name"
    android:scaleType="centerCrop" />

2.先下载后显示
重写NotificationTarget,再获取到图片后再显示通知

总有些坑在等你跳

1.设置横幅后,锁屏后解开直接触发通知的PendingIntent事件

builder.setFullScreenIntent(intent,false);

源码

/**
     * An intent to launch instead of posting the notification to the status bar.
     * Only for use with extremely high-priority notifications demanding the user's
     * <strong>immediate</strong> attention, such as an incoming phone call or
     * alarm clock that the user has explicitly set to a particular time.
     * If this facility is used for something else, please give the user an option
     * to turn it off and use a normal notification, as this can be extremely
     * disruptive.
     *
     * <p>
     * The system UI may choose to display a heads-up notification, instead of
     * launching this intent, while the user is using the device.
     * </p>
     *
     * @param intent The pending intent to launch.
     * @param highPriority Passing true will cause this notification to be sent
     *          even if other notifications are suppressed.
     *
     * @see Notification#fullScreenIntent
     */
    public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
        mFullScreenIntent = intent;
        setFlag(FLAG_HIGH_PRIORITY, highPriority);
        return this;
    }

大概意思就是:设置setFullScreenIntent后,说明这个通知的优先级跟来电话的优先级一样高,会直接显示给你,建议设置开关让用户可以自由选择
因此建议使用设置通知优先级来显示横幅

2.setContentInfo与setContentText

咋一看,貌似区别不大,实际区别如下:

在7.0以上,setContentInfo表示红框区域,setContentText表示黑框区域

/**
     * Set the large text at the right-hand side of the notification.
     */
    public Builder setContentInfo(CharSequence info) {
        mContentInfo = limitCharSequenceLength(info);
        return this;
    }





/**
     * Set the title (first row) of the notification, in a standard notification.
     */
    public Builder setContentTitle(CharSequence title) {
        mContentTitle = limitCharSequenceLength(title);
        return this;
    }

    /**
     * Set the text (second row) of the notification, in a standard notification.
     */
    public Builder setContentText(CharSequence text) {
        mContentText = limitCharSequenceLength(text);
        return this;
    }

    /**
     * Set the third line of text in the platform notification template.
     * Don't use if you're also using {@link #setProgress(int, int, boolean)};
     * they occupy the same location in the standard template.
     * <br>
     * If the platform does not provide large-format notifications, this method has no effect.
     * The third line of text only appears in expanded view.
     * <br>
     */
    public Builder setSubText(CharSequence text) {
        mSubText = limitCharSequenceLength(text);
        return this;
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Notifaction 从4.0到7.0 的相关文章

随机推荐

  • 毕业设计 单片机与OpenMV机器视觉目标跟踪系统

    文章目录 0 前言 课题简介 设计框架 3 openMV实现舵机定位色块STM32 3 硬件设计 4 软件设计 4 1 硬件连接 4 2 软件代码 OpenMV端 4 3 软件代码 STM32端 4 4 利用PC端测试数据数据是否发送接收正
  • 《银行法律法规》一、经济金融基础知识——3、金融市场

    第三章 金融市场 第一节 金融市场概述 考点1 金融市场功能 概念 金融市场是指货币资金融通和金融工具交易的场所 金融市场的融资行为既包括以银行等金融机构为信用媒介的间接融资行为 也包括各类交易主体之间的直接融资行为 主体 是各类融资活动的
  • 运维企业实战Shell脚本合集+万能工具箱

    文章目录 系统维护篇 服务器日常巡检脚本 下线登录用户 企业级Linux日常自动抓取服务器巡检 登录 执行命令记录 备份脚本 终端对话 广播消息 批量查询IP归属地 手机号归属地信息 Linux开机后自动执行命令或脚本 一键自动格式化输出S
  • Anaconda中安装指定版本的tensorflow1.14.0/tensorflow-gpu1.14.0

    在运行github中一个项目时 由于其使用的tensorflow的版本是1 14 0 而我的版本是2 6 0的版本 因为版本过高导致运行失败 所以需要安装tensorflow1 14 0 首先在anaconda的命令行中输入如下命令 pip
  • 【Qt】【CMake】【CMakeLists.txt】-PROJECT_NAME 和 CMAKE_PROJECT_NAME 的区别

    Qt CMake CMakeLists txt PROJECT NAME 和 CMAKE PROJECT NAME 的区别 原帖 https stackoverflow com questions 38938315 difference b
  • 2000+Docker镜像,Kolla是如何管理的

    根据 DockerHub 上的数据 整个 Kolla 项目管理的 镜像有 2000 多个 这么多的镜像 是怎么定义 又是如何构建的呢 简介 我们一直在说的 Kolla 通常情况下泛指 包括了 Kolla 和 Kolla Ansible 两个
  • 二进制部署K8s

    一 环境需求 节点IP 节点名称 所需组件 192 168 248 11 k8s master docker etcd apiserver controller manager scheduler kube proxy flannel 19
  • cobra库:基于cobra-cli命令行生成项目结构

    cobra库 基于cobra cli命令行生成项目结构 一 新建go项目 在F盘创建文件夹cobra started 1 使用mod对go项目进行管理 go mod init cobra started 二 使用cobra cli代码生成
  • 手写嵌入式操作系统(基于stm8单片机)

    include
  • maven学习总结

    众所周知 maven的两大作用是项目构建和依赖管理 除此之外 基于多模块项目 maven常用的功能还有模块化管理 项目构建 Maven是一个构建工具 可以根据项目中的配置文件 pom xml 来自动执行项目的构建过程 它可以将源代码编译 运
  • win10 win7局域网、AD域内共享文件夹方法

    第一 确保访问电脑和被访问电脑同在域中 可右击此电脑 属性 域 查看 第二 确保防火墙关闭 如图均已关闭 第三 选择要共享的文件夹 右击 属性 共享 高级共享 全新 Everyone或指定个人 第四 分享地址 即 本机IP地址 win r输
  • 惠普 g5 服务器 centos安装系统,hp 380G5 安装centos 7

    最近给服务器升级操作系统 发现hp的老机器安装centos 7时不能识别硬盘 原因 hp的服务器G5 使用的是CCISS driver 新的机器使用的是HPSA driver RHEL7 已经移除了 cciss 的支持 处理 安装时候 修改
  • 常数据成员、常成员函数

    定义常数据成员 类型 const 对象名 或者 const 类型 对象名 例如 const clock c1 9 9 9 或者 clock const c2 10 10 10 常对象的几条特殊规则 1 常对象 不能被赋值 2 常对象 不能访
  • 【Pytorch Lighting】第 7 章:半监督学习

    大家好 我是Sonhhxg 柒 希望你看完之后 能对你有所帮助 不足请指正 共同学习交流 个人主页 Sonhhxg 柒的博客 CSDN博客 欢迎各位 点赞 收藏 留言 系列专栏 机器学习 ML 自然语言处理 NLP 深度学习 DL fore
  • 2、halcon+利用光流场检测运动的物体

    这个事例是应用optical flow mg这个算子来在一个图像序列中计算其光溜 并且分割其运动物体 dev update off 把程序窗口 变量窗口 显示窗体变为off状态 dev close window 关闭显示窗口 read im
  • Bert Estimator input_fn 函数调用逻辑

    目录 Bert Estimator input fn 函数调用逻辑 Bert Estimator input fn 函数调用逻辑 网上有很多讲 Bert 源码的 本身代码难度不大 主要两个重点 一个是数据集的处理 以满足 masked LM
  • 整理极客冠军方案

    总结 通常套路就是 1 先观察整体的数据分布 数据集数量 标签数量 标注框数量 数据集数量 数量大 大模型训练缓慢 标签数量 各类的分布情况 gt 分布是否平衡 focal损失函数 长尾分布 样本分布不均衡 mosaic 小目标相对较多 m
  • springboot MongoDB 主从 多数据源

    上一篇 我写了关于用一个map管理mongodb多个数据源 每个数据源 只有单例 的内容 springboot mongodb 配置多数据源 临到部署到阿里云的测试环境 发现还需要考虑一下主从的问题 阿里云买的数据库 不是说让你无感知的 而
  • curl 支持 http2

    源码安装 安装 nghttp2 git clone https github com tatsuhiro t nghttp2 git cd nghttp2 autoreconf i automake autoconf configure m
  • Android Notifaction 从4.0到7.0

    Android Notifaction 从4 0到7 0 图1 4 0 通知样式 图2 5 0 6 0 通知样式 图3 7 0 通知样式 兼容 这么多版本 如何兼容 没关系 交给 android support v7 app Notific