目录

  • 一:普通的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. L2-001. 紧急救援 (Dijkstra算法打印路径)

    作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...

  2. Apple & APPID & iOS & React Native

    Apple & APPID & iOS & React Native 在没有 苹果开发者账号证书 APPID, ios 是否支持导出 app https://developer ...

  3. CSU 1258 异或运算的线段树

    题目大意:在给定区间内对每个数的最后一个二进制为1的位将其修改为0,如果数本身已经为0了,就不做改变 输出给定区间的所有数的异或值 #include <cstdio> #include & ...

  4. [BZOJ3196] [Tyvj1730] 二逼平衡树(线段树 套 Splay)

    传送门 至少BZOJ过了,其他的直接弃. 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的 ...

  5. codeforces 369B

    #include<stdio.h>//题没读懂,没做出来 int main() {      int n,k,l,r,s,s1,m,a,i; while(scanf("%d%d% ...

  6. Ubuntu12.04之修改密码

    Ubuntu 12.04 默认root没有密码 修改密码方式如下: test@localhost:~$ sudo passwd root [sudo] password for test: 输入新的 ...

  7. Avito Code Challenge 2018 C

    C. Useful Decomposition time limit per test 1 second memory limit per test 256 megabytes input stand ...

  8. UVA 10603_Fill

    题意: 给定三个杯子容量,初始只有第三个杯子满,其余为空,求最少需要倒多少水才能让某一杯子中有d升水,如果不能达到,则小于d且尽量接近. 分析: 因为要求转移水量最少,所以采用优先级队列保存每次的状态 ...

  9. 洛谷 P4181 [USACO18JAN]Rental Service

    P4181 [USACO18JAN]Rental Service 题意翻译 farmer john有N(1≤N≤100,000)头牛,他想赚跟多的钱,所以他准备买牛奶和出租牛.有M(1≤M≤100,0 ...

  10. guava cache学习

    Guava Cache与ConcurrentMap很相似,但也不完全一样.最基本的区别是ConcurrentMap会一直保存所有添加的元素,直到显式地移除.相对地,Guava Cache为了限制内存占 ...