目录

  • 一:普通的Notification

    • Notification 的基本操作
    • 给 Notification 设置 Action
    • 更新 Notification
    • 取消 Notification
    • 设置 Notification 的通知效果
      • 铃声
      • 震动
      • 呼吸灯
  • 二: 折叠式(4.0以后出来的)
  • 三:悬挂式
  • 四:其他相关介绍
      1. 多个通知放入到一个组内
      1. 待回复功能的
  • 五:所有API 汇总记录
  • Demo地址

一:普通的Notification

1. Notification 的基本操作

一个 Notification 的必要属性有三项,如果不设置则在运行时会抛出异常:

  • 小图标,通过 setSmallIcon()方法设置
  • 标题,通过 setContentTitle()方法设置
  • 内容,通过setContentText() 方法设置
 private void baseNotify() {
//获取NotificationManager实例
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//实例化NotificationCompat.Builder并设置相关属性
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
//设置小图标
.setSmallIcon(R.mipmap.ic_launcher)
//设置通知标题
.setContentTitle("最简单的Notification")
//设置通知内容
.setContentText("只有小图标、标题、内容");
//通过builder.build()方法生成Notification对象,并发送通知,id=1
if (notifyManager != null) {
notifyManager.notify(1, builder.build());
}
}

2. 给 Notification 设置 Action

 /**
* 发送一个点击跳转到MainActivity的消息
*/
private void action() {
//获取NotificationManager实例
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//获取PendingIntent
Intent mainIntent = new Intent(this, MainActivity.class);
PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
//创建 Notification.Builder 对象
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
//点击通知后自动清除
.setAutoCancel(true)
.setContentTitle("我是带Action的Notification")
.setContentText("点我会打开MainActivity")
.setContentIntent(mainPendingIntent);
//发送通知
notifyManager.notify(2, builder.build());
}

3. 更新 Notification

更新通知很简单,只需要再次发送相同 ID 的通知即可,如果之前的通知还未被取消,则会直接更新该通知相关的属性;如果之前的通知已经被取消,则会重新创建一个新通知。

4. 取消 Notification

  • 点击通知栏的清除按钮,会清除所有可清除的通知
  • 设置了 setAutoCancel()FLAG_AUTO_CANCEL的通知,点击该通知时会清除它
  • 通过NotificationManager 调用cancel(int id) 方法清除指定 ID 的通知
  • 通过NotificationManager调用cancel(String tag, int id) 方法清除指定 TAG 和 ID 的通知
  • 通过NotificationManager 调用cancelAll() 方法清除所有该应用之前发送的通知

5. 设置 Notification 的通知效果

Notification 有震动、响铃、呼吸灯三种响铃效果,可以通过
setDefaults(int defualts) 方法来设置。 Default 属性有以下四种,一旦设置了 Default 效果,自定义的效果就会失效

属性 效果 备注
Notification.DEFAULT_VIBRATE 震动效果 需要申请震动权限<uses-permission android:name="android.permission.VIBRATE" />
Notification.DEFAULT_SOUND 声音效果 设置此值后,调用setSound()设置自定义声音无效
Notification.DEFAULT_LIGHTS 呼吸灯效果 使用时须与 Notification.FLAG_SHOW_LIGHTS 结合使用,否则无效
Notification.DEFAULT_ALL 添加上述三种默认提醒效果
Notification.FLAG_SHOW_LIGHTS 三色灯提醒 过时的方法了
Notification.FLAG_ONGOING_EVENT 发起正在运行事件(活动中)
Notification.FLAG_INSISTENT 让声音、振动无限循环,直到用户响应 (取消或者打开)
Notification.FLAG_ONLY_ALERT_ONCE 发起Notification后,铃声和震动均只执行一次
Notification.FLAG_AUTO_CANCEL 用户单击通知后自动消失
Notification.FLAG_NO_CLEAR 只有调用NotificationManager.cancel()时才会清除
Notification.FLAG_FOREGROUND_SERVICE 表示正在运行的服务

(1) 铃声

 private void init_sound() {
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是伴有铃声效果的通知")
.setContentText("美妙么?安静听~")
//调用自己提供的铃声,位于 /res/values/raw 目录下
.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notice));
if (notifyManager != null) {
notifyManager.notify(2, builder.build());
}
}

(2)震动

/**
* 展示有震动效果的通知,需要在AndroidManifest.xml中申请震动权限
* <uses-permission android:name="android.permission.VIBRATE" />
* 补充:测试震动的时候,手机的模式一定要调成铃声+震动模式,否则你是感受不到震动的
*/
private void init_vibration() {
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long[] vibrate = new long[]{0, 500, 1000, 1500};
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是伴有震动效果的通知")
.setContentText("颤抖吧,凡人~")
.setVibrate(vibrate);
if (notifyManager != null) {
notifyManager.notify(3, builder.build());
}
}

(3)呼吸灯


//我是没测试成功,有知道原因的小伙伴,望告知
private void init_ligths() {
final NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是带有呼吸灯效果的通知")
.setContentText("一闪一闪亮晶晶~")
//ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间
.setLights(0xFF0000, 3000, 3000);
Notification notify = builder.build();
//只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持呼吸灯提醒。
notify.flags = Notification.FLAG_SHOW_LIGHTS;
//使用handler延迟发送通知,因为连接usb时,呼吸灯一直会亮着
notifyManager.notify(4, builder.build());
}

二: 折叠式(4.0以后出来的)

 
Untitled4.gif

一张图说明什么是折叠式

 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void init_remote() {
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("折叠式")
.setContentText("只有小图标、标题、内容"); Notification notification = builder.build();
//就这里需要加上一个属性,R.layout.notity_remote 是展开后的布局
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notity_remote);
notification.bigContentView = remoteViews;
if (notifyManager != null) {
notifyManager.notify(8,notification);
}
}

三:悬挂式

 
Untitled4.gif
 private void init_screen() {
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("悬挂式")
//通过setFullScreenIntent将一个Notification变成悬挂式Notification
.setFullScreenIntent(pendingIntent,true)
//设置Notification的显示等级
.setVisibility(Notification.VISIBILITY_PRIVATE)
.setContentText("只有小图标、标题、内容"); Notification notification = builder.build();
if (notifyManager != null) {
notifyManager.notify(9,notification);
}
}

补充,Notification的显示等级(setVisibility())

属性 说明
VISIBILITY_PRIVATE 表面只有当没有锁屏的时候才能够显示
VISIBILITY_PUBLIC 表明任何情况下都会显示
VISIBILITY_SECRET 表明在pin,password等安全锁和没有锁屏的情况下才能够显示

四:其他相关介绍

1. 多个通知放入到一个组内

 
Untitled4.gif
  int i = 200;
private void init_group() {
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); I++;
String notificationContent = "相同组:" + I;
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().setSummaryText(notificationContent))
.setGroup("EAT")
.setGroupSummary(true);
final Notification notification = builder.build(); notifyManager.notify(10, notification);
}

2. 待回复功能的

 
Untitled4.gif
    @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
private void init_huifu() {
//[1]获取一个NotificationManager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);
//[2]创建remoteInput对象,这个对象指定了这个notification的标题和一个key
String replyLabel = getResources().getString(R.string.app_name);
RemoteInput remoteInput = new RemoteInput.Builder("KEY")
.setLabel(replyLabel)
.build();
//[3]创建一个Action对象 可以指定用户一个友好的输入提示,可以指定跳转意图,
Intent deleteIntent = new Intent(this, MainActivity.class);
Notification.Action action =
new Notification.Action.Builder(R.mipmap.ic_launcher,
"请输入想回复内容", PendingIntent.getActivity(this, 10002, deleteIntent, 0))
.addRemoteInput(remoteInput)
.build(); //[3]创建一个Notification对象
Notification notification =
new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("msg")
.addAction(action)
.build(); //[4]发送这个notification
mNotificationManager.notify(11, notification); }

五:所有API 汇总记录

API 介绍 备注
setSmallIcon 设置小图标
setContentTitle 设置标题
setContentText 设置内容
.setLights() 设置灯光 ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间
setSound 设置自己提供的铃声 Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notice)
setVibrate 设置震动 long[] vibrate = new long[]{0, 500, 1000, 1500};
setAutoCancel() 点击通知后自动清除 true自动清除
setFullScreenIntent 将一个Notification变成悬挂式Notification 具体使用看上面的介绍
setVisibility 设置Notification的显示等级 看上面 悬挂式
setContentIntent 设置点击事件 查看上面 给 Notification 设置 Action
setDefaults 设置 Notification 的通知效果 查看上面的 5

最后

GitHub 项目地址 点击跳转

感谢

http://blog.csdn.net/axi295309066/article/details/52929879


作者:Allens_Jiang
链接:https://www.jianshu.com/p/bdc132bdb0fa
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Notification操作大全的更多相关文章

  1. Delphi Excel 操作大全

    Delphi Excel 操作大全 (一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObj ...

  2. java日期操作大全

    摘自(http://www.blogjava.net/i369/articles/83483.html) java日期操作 大全 先来一个:  取得指定月份的第一天与取得指定月份的最后一天  http ...

  3. PHP数组操作大全

    <?php /** * File: phpstudy : array_test.php * Created by PhpStorm. * User: IhMfLy Pheonix@jtv-070 ...

  4. Java 文件操作大全

    Java 文件操作大全 //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if (!myFolderPat ...

  5. SQL语句操作大全

    SQL语句操作大全   本文分为以下六个部分: 基础部分 提升部分 技巧部分 数据开发–经典部分 SQL Server基本函数部分 常识部分 一.基础 1.说明:创建数据库CREATE DATABAS ...

  6. c#数据库操作大全

    原文:c#数据库操作大全 1.提取单条记录 //using System.Data; //using System.Data.SqlClient;  using (SqlConnection cn = ...

  7. MATLAB命令大全和矩阵操作大全

    转载自: http://blog.csdn.net/dengjianqiang2011/article/details/8753807 MATLAB矩阵操作大全 一.矩阵的表示在MATLAB中创建矩阵 ...

  8. SQLite3命令操作大全

    SQLite3命令操作大全 SQLite库包含一个名字叫做sqlite3的命令行,它可以让用户手工输入并执行面向SQLite数据库的SQL命令.本文档提供一个样使用sqlite3的简要说明. 一.ql ...

  9. Python文件操作大全

    Python 编程文件操作大全   文件打开模式 打开模式 执行操作 'r' 以只读方式打开文件(默认) 'w' 以写入的方式打开文件,会覆盖已存在的文件 'x' 如果文件已经存在,使用此模式打开将引 ...

随机推荐

  1. Python 函数递归-三元表达式-列表生成式-字典生成式-匿名函数-内置函数

    上节课复习: 1. 无参装饰器 def 装饰器名字(func): def wrapper(*args,**kwargs): res = func(*args,**kwargs) return res ...

  2. [Python3网络爬虫开发实战] 4-解析库的使用

    上一章中,我们实现了一个最基本的爬虫,但提取页面信息时使用的是正则表达式,这还是比较烦琐,而且万一有地方写错了,可能导致匹配失败,所以使用正则表达式提取页面信息多多少少还是有些不方便. 对于网页的节点 ...

  3. [Python3网络爬虫开发实战] 1.7.1-Charles的安装

    Charles是一个网络抓包工具,相比Fiddler,其功能更为强大,而且跨平台支持得更好,所以这里选用它来作为主要的移动端抓包工具. 1. 相关链接 官方网站:https://www.charles ...

  4. 使用 PHP + shell 生成 一键设置权限的脚本。

    linux 系统 支持PHP脚本一键设置环境.shell脚本一键设置环境.那么 我今天 使用 PHP  + shell 生成 一键设置权限的脚本. 举例子:linux服务器 一键配置discuz网站环 ...

  5. Node.js & Unix/Linux & NVM

    Node.js & Unix/Linux & NVM nvm https://github.com/creationix/nvm https://github.com/xyz-data ...

  6. Thinkphp5.0 的Model模型

    Thinkphp5.0 的Model模型 新建user模型User.php: <?php namespace app\index\model; use think\Model; class Us ...

  7. 安装adt-bundle-windows-x86-20130917时遇到的问题及解决方法

    最近在上安卓课,老师让我们下载此软件(adt-bundle-windows-x86-20130917.下载压缩后,打开eclipse的时候,会出现以下情况: 这时说明你的jdk还没下载或者下载错位置了 ...

  8. neutron trouble shooting - ip can not ping

    neutron创建了一个router后,显示列表如下: [root@controller01 keystone]# neutron router-port-list router +--------- ...

  9. pycharm里html注释是{# #}而不是<!-- -->?

    修改方式:如图修改成值None以后,command+/快捷键,html注释的符号就是<!-- 注释内容 -->:为Jinja2的时候,注释符号就是{# 注释内容 #} 修改成None时,H ...

  10. PHP关于文件与文件夹(1) 写入文件 文件权限 三、锁定文件

    一.文件权限 总之中的一个切都是为了保证文件夹的安全,保证文件夹的安全比保证文件的安全更重要. 二.写入文件 file_put_contents($file,$data);  //假设没有的话会创建. ...