Notification-通知

通知是一种消息,这种消息呈现在应用UI之外,通过通知可以对用户进行提醒、可以和其他用户收发信息等。通过点击通知,可以唤起app页面或者直接在通知上执行一些操作。下面介绍通知的一些常用用法。

展示一条普通通知

先来看一个效果:

从这张截图上,可以看到有一条通知消息,如果要实现这个效果,参考以下实现逻辑:

private static final String CHANNEL_ID_1 = "CHANNEL_ID_1";public void doNotify(View v) {NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationChannel channel = new NotificationChannel(CHANNEL_ID_1, CHANNEL_NAME_1, NotificationManager.IMPORTANCE_DEFAULT);notificationManagerCompat.createNotificationChannel(channel);}final String content = "独行侠大胜太阳拖入“抢七” 热火击败76人挺进东决";NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID_1);builder.setSmallIcon(android.R.drawable.ic_dialog_alert).setContentTitle("通知标题").setContentText(content).setPriority(NotificationCompat.PRIORITY_DEFAULT);final int notificationId = 101;notificationManagerCompat.notify(notificationId, builder.build());
}

这里有几点需要说明:

  1. NotificationManagerCompat是对NotificationManager进行了版本差异化的封装,本质为(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
  2. Android 8.0开始,发送任何通知前,都要先创建通知通道NotificationChannel;创建一个已存在的通知通道会被忽略,因此重复创建通知通道是安全;官方推荐是app启动时创建通道;
    创建通知通道NotificationChannel的参数需要注意2点:

    • channelId包内唯一;
    • important参数,不同级别对用户的提醒方式和打扰程度不同如声音等,详情可以参考NotificationManager中IMPORTANCE_*说明,这里使用默认important;
  3. 创建单个通知渠道notificationManagerCompat.createNotificationChannel(channel)实际执行了mNotificationManager.createNotificationChannel(channel)(除了提供创建单个通知渠道外,内部还提供了创建多个通知渠道的重载方法:public void createNotificationChannels(@NonNull List<NotificationChannel> channels));
  4. NotificationCompat.Builder: NotificationCompatNotification进行了版本差异化封装,采用建造者模式创建Notification;
    • builder.setSmallIcon(R.mipmap.ic_launcher): 设置通知小图标,这是一条通知的必须元素!
    • setPriority(NotificationCompat.PRIORITY_DEFAULT): 7.1及以下版本,通过此方法设置通知权重,即打扰用户程度;8.0及以上版本,需要用NotificationChannel来设置相关属性。
  5. notificationManagerCompat.notify(notificationId, builder.build()): 注意notificationId必须唯一,之后可对该通知进行更新,如下载进度更新;

这是一条最简单的通知,还没有给通知添加用户行为,下面对其增加点击交互。

通知点击交互

先看效果

从图上可以看到,A页面只有一个发送通知的按钮,而在成功发送通知并点击通知后,A页面展示了通知上的内容,通知也随即消失。要实现这个功能,只需要改造上一节的实例代码如下:

  1. AndroidManifest.xml修改启动模式:android:launchMode="singleTop";

  2. doNotify()方法调整:

    public void doNotify(View v) {NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationChannel channel = new NotificationChannel(CHANNEL_ID_1, CHANNEL_NAME_1, NotificationManager.IMPORTANCE_DEFAULT);notificationManagerCompat.createNotificationChannel(channel);}final String content = "独行侠大胜太阳拖入“抢七” 热火击败76人挺进东决";Intent intent = new Intent(this, NotificationActivity.class);intent.putExtra("content", content);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID_1);builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("通知标题").setContentIntent(pendingIntent).setContentText(content).setPriority(NotificationCompat.PRIORITY_DEFAULT).setAutoCancel(true);final int notificationId = 101;notificationManagerCompat.notify(notificationId, builder.build());
    }
    

    这里有几点需要说明:

    • PendingIntent: 将Intent封装为PendingIntent, 并且标记设置flagsPendingIntent.FLAG_UPDATE_CURRENT用以设置Intent中的额外参数;
    • 如果将flags设置为PendingIntent.FLAG_IMMUTABLE,则intent中的附加参数将不会生效;
  3. Activity中增加onNewIntent()回调逻辑:

    protected void onNewIntent(Intent intent) {super.onNewIntent(intent);if (intent.hasExtra("content")) {((TextView) findViewById(R.id.tv_notification_content)).setText(intent.getStringExtra("content"));}
    }
    

    上面的例子中,可以发现消息内容被截取成了单行显示,接下来展示如何显示完整的消息。

可收起和展开的消息

文本消息:

看效果

要实现可展开收起的消息比较简单,在上述代码基础上添加一行代码即可setStyle(new NotificationCompat.BigTextStyle().bigText(content))

...NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID_1);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), android.R.drawable.star_big_on);
builder.setSmallIcon(android.R.drawable.stat_notify_chat).setLargeIcon(bitmap).setContentTitle("通知标题").setContentText(content).setStyle(new NotificationCompat.BigTextStyle().bigText(content)).setPriority(NotificationCompat.PRIORITY_DEFAULT).setContentIntent(pendingIntent).setAutoCancel(true);final int notificationId = 101;
notificationManagerCompat.notify(notificationId, builder.build());
...

这里还增加了setLargeIcon(bitmap)来设置通知的大图标。

图片通知

类似的还可以设置一张可收起展开的图片通知,设置style:setStyle(new NotificationCompat.BigPictureStyle().bigLargeIcon(null).bigPicture(bitmap))

以上就是通知的常用场景,当然通知还有一些高级用法,比如和前台服务结合的下载进度条、媒体控制等。

Notification-通知相关推荐

  1. 配置 SQL Server 2008 Email 发送以及 Job 的 Notification通知功能

    SQL Server 2008配置邮件的过程就不写了,网上的案例太多了. http://www.cnblogs.com/woodytu/p/5154526.html 这个案例就不错. 主要写下配置完后 ...

  2. 安卓APP_ 控件(6)—— Notification通知

    摘自:安卓APP_ 控件(6)-- Notification通知 作者:丶PURSUING 发布时间: 2021-04-02 00:30:14 网址:https://blog.csdn.net/wei ...

  3. Android Notification通知详解

    Android Notification通知详解 Notification: (一).简介: 显示在手机状态栏的通知.Notification所代表的是一种具有全局效果的通知,程序一般通过Notifi ...

  4. Android Notification通知详细解释

    Android Notification通知具体解释 Notification: (一).简单介绍: 显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通知,程序一般通过N ...

  5. Android学习|控件——Notification通知

    Android学习|控件--Notification通知 一.前提 二.两个对象的的构建 1.创建NotificationManager 2.使用Builder构造器来创建Notification 2 ...

  6. Android学习日记 Notification 通知

    Android学习日记 Notification 通知 文章目录 Android学习日记 Notification 通知 前言 使用步骤 总结 前言 下拉状态栏显示的通知功能 使用步骤 代码如下: p ...

  7. js Notification 通知

    notification MDN文档 https://developer.mozilla.org/zh-CN/docs/Web/API/notification 实现一个通知 // 浏览器支持且用户没 ...

  8. 【黑马Android】(11)音乐播放器/视频播放器/照相机/常见对话框/notification通知/样式和主题/帧动画/传感器/应用程序反编译与安装

    音乐播放器api <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns: ...

  9. 适配 通知 Notification 通知渠道 前台服务 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  10. Notification —— 通知

             Notification -- 通知,是一种让你的应用程序在不使用Activity的情况下警示用户.它是看不见的程序组件(Broadcast Receiver,Service和不活跃 ...

最新文章

  1. SQLServer出现 '其他会话正在使用事务的上下文' 的问题原因,什么是环回链接服务器?(转载)...
  2. 小师妹学JVM之:cache line对代码性能的影响
  3. [Unity2D]游戏引擎介绍
  4. 第二十三篇:在SOUI中使用LUA脚本开发界面
  5. 中国水溶性PVA薄膜行业市场供需与战略研究报告
  6. SQLSERVER语句的执行时间
  7. tomcat安装-tomcat8.5
  8. 正则表达式 —— 括号与特殊字符
  9. LAMP+Varnish缓存详解(一)——Varnish简介
  10. AIM Tech Round 5 (rated, Div. 1 + Div. 2)
  11. Atitit 人工智能目前的进展与未来 包含的技术 v3
  12. java fri星期转_Java日期时间以及日期相互转换
  13. python绘制聚类分析树状图
  14. android 静默暗转_Android PackageInstaller 静默安装的实现
  15. 禾多科技与RTI达成合作,加速自动驾驶在中国量产落地
  16. 不用验证,下载wmp10
  17. 淘宝天猫京东拼多多苏宁抖音等平台关键词监控价格API接口(店铺商品价格监控API接口调用展示)
  18. struct sockaddr与struct sockaddr_in ,struct sockaddr_un的区别和联系
  19. echart折线图连线不显示问题总结
  20. Danmo 的学习之路(HTML——CSS)

热门文章

  1. Soot -- Soot中的一些语句细节
  2. 卷积神经网络相比循环神经网络具有哪些特征
  3. 电路邱关源学习笔记——1.7基尔霍夫定律
  4. Linux命令find -perm使用方法
  5. 2020-11-30 09:51:55 精确到秒的时间戳
  6. 用bat执行ps1脚本
  7. Bootstrap 组件 Button 按钮
  8. 3D人物移动和相机跟随_学习整理资料
  9. strstr函数及模拟
  10. python制作九宫图