场景

App在接收到后台推送的消息后,需要在系统通知栏中显示通知消息,并且点击通知消息跳转到新的页面,并将消息内容传递过去。

效果如下

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

首先在主页面添加一个按钮

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Buttonandroid:id="@+id/showNotice"android:text="显示通知"android:layout_width="match_parent"android:layout_height="wrap_content"/></RelativeLayout>

然后在activity中设置按钮的点击事件

    private Button showNoticeButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showNoticeButton = findViewById(R.id.showNotice);showNoticeButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {showNotification(MainActivity.this, "通知内容:关注公众号,编程资料与技术共享学习");}});}

在点击事件中调用了方法showNotification并且传递了消息的内容,在方法showNotification中

    public void showNotification(Context context, String content) {//1.创建通知管理器NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationCompat.Builder builder;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//Android 8.0版本适配NotificationChannel channel = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_HIGH);notificationManager.createNotificationChannel(channel);builder = new NotificationCompat.Builder(context, "default");} else {builder = new NotificationCompat.Builder(context);}//用来进行跳转并传递消息的intentIntent intent = new Intent(this, NoticeActivity.class);intent.putExtra("content",content);//2.创建通知实例Notification notification = builder.setContentTitle("通知标题:公众号-霸道的程序猿").setContentText(content).setWhen(System.currentTimeMillis())//smallIcon 通知栏显示小图标//android5.0 之后通知栏图标都修改了,小图标不能含有RGB图层,也就是说图片不能带颜色,否则显示的就成白色方格了//解决方法一:为图片带颜色,targetSdkVersion改为21以下//解决方法二:只能用白色透明底的图片.setSmallIcon(R.mipmap.ic_launcher)//LargeIcon 下拉后显示的图标.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//收到通知时的效果,这里是默认声音.setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true).setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)).build();//3.notify//notifyId每次要不一致,不然下一次的通知会覆盖上一次int notifyId = new Random().nextInt();notificationManager.notify(notifyId, notification);}

首先创建通知管理器,然后创建通知实例,在创建通知实例时传递了一个Intent对象用来进行点击通知消息的跳转,通过

        //用来进行跳转并传递消息的intentIntent intent = new Intent(this, NoticeActivity.class);intent.putExtra("content",content);

传递要显示的消息内容

然后新建一个Activity叫NoticeActivity用来实现点击通知后跳转显示消息内容。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".NoticeActivity"><TextViewandroid:id="@+id/content_TextView"android:layout_width="match_parent"android:layout_height="wrap_content"/></RelativeLayout>

添加一个TextView用来显示消息内容

然后在Activity中

    @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_notice);String content = getIntent().getStringExtra("content");TextView textView = findViewById(R.id.content_TextView);textView.setText("消息内容:\n"+content);}

获取传递的参数并显示消息内容。

效果

点击显示通知按钮

点击通知内容

Android中使用Notification在通知栏中显示通知相关推荐

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

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

  2. Android 12.0 SystemUI状态栏屏蔽掉通知栏不显示通知

    目录 1.概述 2.SystemUI状态栏屏蔽掉通知栏不显示通知的核心类

  3. Android 10.0 SystemUI状态栏屏蔽掉通知栏不显示通知

    由于客户需求不需要通知,所以要求去掉所有通知,而通知部分就是在SystemUI部分管理的,所以就要从这里入手来去掉关于通知栏的部分 首选要从两部分入手 第一部分,状态栏显示通知图标的部分 状态栏布局为 ...

  4. SystemUI 勿扰模式下来电通知栏不显示通知

    不积跬步无以至千里 一.前摘 当你开启勿扰模式的时候,此刻你肯定不想被打扰,无论是声音还是显示上,那此时的来电通知会怎样呢?毕竟来电通知是一个非常重要的通知,那它是在勿扰模式中怎么被处理的呢? 二.正 ...

  5. android 通知栏样式_Android通知样式

    android 通知栏样式 We've discussed and implemented basic Notifications in this post. In this tutorial, we ...

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

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

  7. android下载通知栏,Android开发中实现下载文件通知栏显示进度条

    android开发中实现下载文件通知栏显示进度条. 1.使用asynctask异步任务实现,调用publishprogress()方法刷新进度来实现(已优化) public class myasync ...

  8. 小米android通知栏提示怎么关闭,怎么取消小米音乐自动在通知栏中显示控制开?...

    在MIUI系统更新之后,小米音乐在使用的时候会自动在通知栏中显示控制开关,非常不方便,那么我们该怎么将其屏蔽呢?欢迎大家速来围观我的经验啦. 一.在MIUI系统设置中进行屏蔽 1.虽然在通知栏中可以直 ...

  9. Android中的Notification

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

最新文章

  1. 一款遥控器拆解之后可利用的元器件
  2. openoffice 安装后中文字符乱码问题
  3. [BSidesSF2020]haystack
  4. FreeMarker整合Spring 3
  5. 快速的CSV文件生成器
  6. 深入解析Node.js setTimeout方法的执行过程
  7. SH760二自由度系统非周期激励解析法求解
  8. 【操作系统】多道程序的理解
  9. Intel Sandy Bridge/Ivy Bridge架构/微架构/流水线 (15) - L1数据缓存/读写地址转换
  10. 源服务器未能找到目标资源的表示或者不愿,java - 源服务器没有找到目标资源的当前表示,或者不愿意透露一个存在。 关于部署到 tomcat - 堆栈内存溢出...
  11. windows 下nodejs 升级无法通过n模块升级
  12. 工程图样中粗实线的用途_电气工程图的一般特点、设计规范
  13. telnet相似命令 linux,Linux telnet命令
  14. python开发浏览器_Python制作简易浏览器
  15. 维宏云智能工厂系统1.0全面升级,带你体验豪华智能制造
  16. matlab四叉树分割代码,【测绘专用】 MATLAB 四叉树分割遥感图像
  17. 【Python PIL】用PIL绘制中国象棋棋盘
  18. 在微信小游戏中使用tensorflow的face-landmarks-detection
  19. 成为顶流平台后 新氧阳谋峥嵘显露
  20. ios 新浪微博网页登陆分享 {error:userinfoerror pos:5}

热门文章

  1. linux——管理系统存储之设备的识别、发现、挂载
  2. SpringBoot v2.2.6版本遇到的坑 --- HiddenHttpMethodFilter组件的锅
  3. Java DelayQueue延迟队列的使用和源码分析
  4. Elasticsearch 100问(下)
  5. J.U.C系列(二)CyclicBarrier的使用
  6. spring service ,controller反向代理生成AOP代理类流程
  7. adapter pattern java_适配器模式(Adapter Pattern)
  8. java jxls 科学计数_java通过jxls框架实现导入导出excel
  9. 内向者优势 原版_未来内向的人具有越来越高的竞争力——心理学:学会运用性格优势...
  10. win8网速怎么测试软件,Windows8小技巧:查看当前网速