文章目录

  • 什么是通知
  • 一个发送通知的栗子:
  • Android 8.0不能弹出通知
  • 一个兼容8.0的栗子
  • 让通知常驻通知栏
  • 如何清除通知
  • 如何更新通知界面内容
  • 点击通知执行意图
  • 如何实现进度条的通知
  • 如何自定义通知的UI界面

什么是通知

通知是 Android 中 Service 与用户交互的一种方式(主要是 Service )

如何发送一个简单通知?
1、获取 NotificationManager 通知管理器
2、构建 Notification 对象:
使用 Notification.Builder 构建器构建对象

Builder builder = new Builder();
builder.setXXX().setXXX().build();

3、调用 manager.notify() 方法发送该通知

一个发送通知的栗子:

private static final int NOTIFICATION_ID = 1001;
private void sendNotification() {//1、NotificationManagerNotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);/** 2、Builder->Notification*  必要属性有三项*  小图标,通过 setSmallIcon() 方法设置*  标题,通过 setContentTitle() 方法设置*  内容,通过 setContentText() 方法设置*/Notification.Builder builder = new Notification.Builder(this);builder.setContentInfo("Content info").setContentText("Content text")//设置通知内容.setContentTitle("Content title")//设置通知标题.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)).setSmallIcon(R.mipmap.ic_launcher_round)//不能缺少的一个属性.setSubText("Subtext").setTicker("滚动消息......").setWhen(System.currentTimeMillis());//设置通知时间,默认为系统发出通知的时间,通常不用设置Notification n = builder.build();//3、manager.notify()manager.notify(NOTIFICATION_ID,n);
}

运行在API22的手机上效果:

Android 8.0不能弹出通知

运行在API26的手机上怎么样呢…原来根本就不能弹出通知,填坑请戳下文:
Notification Android8.0中无法发送通知,提示:No Channel found for pkg

一个兼容8.0的栗子

private static final int NOTIFICATION_ID = 1001;
private void sendNotification() {//1、NotificationManagerNotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);/** 2、Builder->Notification*  必要属性有三项*  小图标,通过 setSmallIcon() 方法设置*  标题,通过 setContentTitle() 方法设置*  内容,通过 setContentText() 方法设置*/Notification.Builder builder = new Notification.Builder(this);builder.setContentInfo("Content info").setContentText("Content text")//设置通知内容.setContentTitle("Content title")//设置通知标题.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)).setSmallIcon(R.mipmap.ic_launcher_round)//不能缺少的一个属性.setSubText("Subtext").setTicker("滚动消息......").setWhen(System.currentTimeMillis());//设置通知时间,默认为系统发出通知的时间,通常不用设置if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {NotificationChannel channel = new NotificationChannel("001","my_channel",NotificationManager.IMPORTANCE_DEFAULT);channel.enableLights(true); //是否在桌面icon右上角展示小红点channel.setLightColor(Color.GREEN); //小红点颜色channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知manager.createNotificationChannel(channel);builder.setChannelId("001");}Notification n = builder.build();//3、manager.notify()manager.notify(NOTIFICATION_ID,n);
}

让通知常驻通知栏

如何让通知常驻通知栏?(用户无法删除)
第一种方式:

//让通知常驻通知栏
builder.setOngoing(true);

第二种方式:

Notification n = builder.build();
n.flags = Notification.FLAG_NO_CLEAR;

如何清除通知

 private void clearNotification() {//单利的系统服务NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);manager.cancel(NOTIFICATION_ID);}

如何更新通知界面内容

如何更新通知的界面内容?
更新界面中的内容原理就是使用相同的 ID 再次发送一个内容不同的通知即可

    private static final int NOTIFICATION_ID2 = 1002;private int progress = 0;private void sendNotification2() {NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);Notification.Builder builder = new Notification.Builder(this);builder.setContentTitle("音乐下载").setContentText("下载进度:"+progress+"%").setSmallIcon(R.mipmap.ic_launcher)                ;if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {NotificationChannel channel = new NotificationChannel("002","download_channel",NotificationManager.IMPORTANCE_DEFAULT);manager.createNotificationChannel(channel);builder.setChannelId("002");}Notification n = builder.build();manager.notify(NOTIFICATION_ID2,n);progress+=10;}

点击通知执行意图

如何实现点击了通知后,执行具体意图?
builder.setOntentIntent(PendingIntent);

PendingIntent 是一个包装意图,该对象中包含了具体的意图, PendingIntent 可以传递给另外的应用程序,让对让在某个时刻执行该 PendingIntent

如何获取 PendingIntent?

PendingIntent pi = PendingIntent.getActivity(...);
PendingIntent pi = PendingIntent.getService(...);
PendingIntent pi = PendingIntent.getBroadcast(...);

sendNotification2方法修改如下:

Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
//给通知添加点击意图
builder.setContentIntent(pi);

如何实现进度条的通知

如何实现进度条的通知?

builder.setProgress(max,progress,indetermination);
setProgress(0,0,false);去掉进度条
setProgress(100,20,false);带有进度条
setProgress(10,20,true);不确定进度条
private void sendProgressNotification() {final NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);final Notification.Builder builder = new Notification.Builder(this);builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("进度").setContentText("进度...").setProgress(100,10,true);if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {NotificationChannel channel = new NotificationChannel("003","download_channel",NotificationManager.IMPORTANCE_DEFAULT);manager.createNotificationChannel(channel);builder.setChannelId("003");}Notification n = builder.build();manager.notify(NOTIFICATION_ID3,n);//每隔1秒更新进度条进度//启动工作线程new Thread(){@Overridepublic void run() {try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}for(int i=1;i<=10;i++){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}//发通知builder.setProgress(100,i*10,false);Notification n = builder.build();manager.notify(NOTIFICATION_ID3,n);}//更新通知内容manager.cancel(NOTIFICATION_ID3);builder.setProgress(0,0,false);builder.setContentText("音乐下载完毕");Notification n = builder.build();manager.notify(NOTIFICATION_ID3,n);}}.start();}

如何自定义通知的UI界面

如何自定义通知的 UI 界面?

RemoteViews views = new RemoteViews(...,...);
builder.setContent(RemoteViews view);

如何给自定义的通知上的按钮添加点击意图?

RemoteViews views = new RemoteViews(...,...);
views.setOnclickPendingIntent(id,PendingIntent);


public class NotificationActivity extends Activity implements View.OnClickListener{private static final int NOTIFICATION_ID3 = 1003;private static final int NOTIFICATION_ID4 = 1004;Button button;Button button2;private MyReceiver receiver;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_notification);button = findViewById(R.id.button);button.setOnClickListener(this);button2 = findViewById(R.id.button2);button2.setOnClickListener(this);//动态注册广播接收器receiver = new MyReceiver();IntentFilter intentFilter = new IntentFilter();intentFilter.addAction("ACTION_BUTTON_PAUSE_CLICKED");this.registerReceiver(receiver,intentFilter);}class MyReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {Log.i("info","接收到了广播"+intent.getAction());}}@Overrideprotected void onDestroy() {this.unregisterReceiver(receiver);super.onDestroy();}@Overridepublic void onClick(View view) {switch (view.getId()){case R.id.button:sendProgressNotification();break;case R.id.button2:sendCustomeNotification();break;}}public void sendCustomeNotification() {NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);Notification.Builder builder = new Notification.Builder(this);builder.setSmallIcon(R.mipmap.ic_launcher);RemoteViews views = new RemoteViews(getPackageName(),R.layout.layout_notification);//给views中的按钮添加点击意图Intent intent = new Intent(this,NewsContentActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);views.setOnClickPendingIntent(R.id.btn_pre,pendingIntent);//给中间按钮添加点击意图Intent intent1 = new Intent("ACTION_BUTTON_PAUSE_CLICKED");PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this,0,intent1,PendingIntent.FLAG_UPDATE_CURRENT);views.setOnClickPendingIntent(R.id.btn_pause,pendingIntent1);builder.setContent(views);if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {NotificationChannel channel = new NotificationChannel("004","download_channel",NotificationManager.IMPORTANCE_DEFAULT);manager.createNotificationChannel(channel);builder.setChannelId("004");}Notification n = builder.build();manager.notify(NOTIFICATION_ID4,n);}private void sendProgressNotification() {final NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);final Notification.Builder builder = new Notification.Builder(this);builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("进度").setContentText("进度...").setProgress(100,10,true);if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {NotificationChannel channel = new NotificationChannel("003","download_channel",NotificationManager.IMPORTANCE_DEFAULT);manager.createNotificationChannel(channel);builder.setChannelId("003");}Notification n = builder.build();manager.notify(NOTIFICATION_ID3,n);//每隔1秒更新进度条进度//启动工作线程new Thread(){@Overridepublic void run() {try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}for(int i=1;i<=10;i++){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}//发通知builder.setProgress(100,i*10,false);Notification n = builder.build();manager.notify(NOTIFICATION_ID3,n);}//更新通知内容manager.cancel(NOTIFICATION_ID3);builder.setProgress(0,0,false);builder.setContentText("音乐下载完毕");Notification n = builder.build();manager.notify(NOTIFICATION_ID3,n);}}.start();}
}

【达内课程】Android中的Notification相关推荐

  1. Android中的Notification

    原帖地址:http://www.cnblogs.com/newcj/archive/2011/03/14/1983782.html Notification 的使用需要导入 3 个类 1 2 3 im ...

  2. android 大视图风格通知栏,Android中使用Notification实现宽视图通知栏(Notification示例二)...

    Notification是在你的应用常规界面以外展现的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情须要进入通知抽屉(notificationdrawer) ...

  3. Android中使用Notification在通知栏中显示通知

    场景 App在接收到后台推送的消息后,需要在系统通知栏中显示通知消息,并且点击通知消息跳转到新的页面,并将消息内容传递过去. 效果如下 注: 博客: https://blog.csdn.net/bad ...

  4. xamarin android 通知,在 Xamarin.Android 中使用 Notification.Builder 构建通知

    0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...

  5. Android中使用Notification在状态栏上显示通知

    场景 状态栏上显示通知效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 ...

  6. android notification的使用方法,详解Android中Notification的使用方法

    在消息通知的时候,我们经常用到两个控件Notification和Toast.特别是重要的和需要长时间显示的信息,用Notification最合适不过了.他可以在顶部显示一个图标以标示有了新的通知,当我 ...

  7. android notification 的总结分析,Android中Notification用法实例总结

    本文实例总结了 Android中Notification用法.分享给大家供大家参考,具体如下: 我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图 ...

  8. Android中的网络编程-黄俊东-专题视频课程

    Android中的网络编程-1902人已学习 课程介绍         剔除繁杂的理论,注重实践,深入浅出讲解Android中的网络编程 课程收益     每一个学员都能系统地掌握Android中的网 ...

  9. 【达内课程】Android入门知识

    文章目录 当新建一个 Android 项目时 关于布局和控件 语言的国际化自适应 屏幕尺寸自适应 style样式 简单登录界面实现 课程到这里我们讲了许多 Java 的知识,也学习了 Android ...

最新文章

  1. 用户画像|产品经理应该如何定位用户
  2. spring-security-学习笔记-02-基于Session的认证方式
  3. 通过Etcd+Confd自动管理Haproxy(多站点)
  4. 迪士尼确认《花木兰》档期:7月24日北美等多地上映
  5. 未来 10 年,物联网将成为主流!
  6. 安装过mysql和p_MAC下安装与配置MySQL
  7. 帮Python找“对象”
  8. STELLA—系统动力学仿真软件 System Dynamics仿真
  9. 软件工程专业的论文答辩_软件工程专业本科毕业答辩?
  10. 正则判断数字加下划线加数字
  11. 云林深处,一场灵魂的宿醉
  12. 4227. 【五校联考3day2】B (Standard IO)
  13. 数字逻辑电路——反演规则与对偶规则
  14. php pandoc,Pandoc 标记语言转化工具
  15. DisparityCost Volume in Stereo
  16. lda指令是什么意思_汇编指令大全
  17. Java项目中使用Freemarker生成Word文档
  18. Python人脸微笑识别2-----Ubuntu16.04基于Tensorflow卷积神经网络模型训练的Python3+Dlib+Opencv实现摄像头人脸微笑检测
  19. 淘宝商品详情接口,淘宝详情页接口,宝贝详情页接口,商品属性接口,商品信息查询,商品详细信息接口,h5详情,淘宝APP详情
  20. Cookie的修改、删除,清除

热门文章

  1. 【ST-Link 烧入问题】
  2. 独家分享PC端和无线端直通车质量得分快速提高的技巧!
  3. 关于HTTP头域User-Agent二三事
  4. 在 Linux 里实现 FriendlayARM 提供的 SD-Flasher.exe工具的功能
  5. Java 七牛缓存刷新
  6. 基于Java的电梯模拟系统
  7. Mac及Linux安装Autodock及ADT
  8. 【中秋】纯CSS实现日地月的公转
  9. 用spss实现KNN(K近邻)算法
  10. 神经网络前向传播算法