当前位置: 首页 > news >正文

android使用notification消息通知(工具类封装)

代码直接复制粘贴就可以用了,参数可以更具自己需求添加

private NotificationManager manager;private Notification notification;private static final String NORMAL_CHANNEL_ID = "my_notification_normal";private static final String IMPORTANT_CHANNEL_ID = "my_notification_important";/*** 实现包装方法(参数按自己需求来)* @param title 标题* @param content 内容* @param priorityType 通知级别:0为重要通知(有提示音),非0为普通通知(无提示音)*/public void sendNotify (String title, String content, int priorityType) {manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationChannel wsschannel; // channel(安卓8.0及以上才用到)String channel_id = IMPORTANT_CHANNEL_ID; // channelId用于绑定builder配置int priority = NotificationCompat.PRIORITY_LOW; // 通知级别// 判断是否8.0及以上版本,并选择发送消息的级别if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {if (priorityType == 0) {wsschannel = new NotificationChannel(IMPORTANT_CHANNEL_ID, "重要通知", NotificationManager.IMPORTANCE_HIGH);channel_id = IMPORTANT_CHANNEL_ID;} else {wsschannel = new NotificationChannel(NORMAL_CHANNEL_ID, "一般通知", NotificationManager.IMPORTANCE_LOW);channel_id = NORMAL_CHANNEL_ID;}manager.createNotificationChannel(wsschannel);}// 通知级别if (priorityType == 0) {priority = NotificationCompat.PRIORITY_HIGH;} else {priority = NotificationCompat.PRIORITY_LOW;}// 点击意图Intent intent = new Intent(this, MainActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);// 通知配置notification = new NotificationCompat.Builder(this, channel_id).setContentTitle(title) // 标题.setContentText(content) // 内容.setSmallIcon(R.mipmap.user) // 小图标.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.setting)).setPriority(priority) // 7.0 设置优先级.setContentIntent(pendingIntent) // 跳转意图.setAutoCancel(true) // 点击后自动销毁.build();// 给每个通知随机idint id = (int) ((Math.random() * 9 + 1) * (6));// 生成通知manager.notify(id , notification);}
// 示例
sendNotify("服务关闭", "服务关闭成功", 0);

http://www.lryc.cn/news/229454.html

相关文章:

  • PicoDiagnostics (NVH设备软件)-PS软件设置文件类型介绍
  • Linux 定时删除7天前的文件
  • VISA机制
  • 基于开源项目OCR做一个探究(chineseocr_lite)
  • 工作常遇,Web自动化测试疑难解答,测试老鸟带你一篇打通...
  • H5判断当前环境是否为微信小程序
  • 桌面云架构讲解(VDI、IDV、VOI/TCI、RDS)
  • 计算图片中两个任意形状多边形相交部分的大小
  • JavaScript的函数
  • stm32 - Cortex
  • 计算机组成原理之概述
  • 思维导图软件 Xmind mac中文版软件特点
  • 群晖 DSM 7.0 Synology Photos IOS | Android 客户端下载
  • @CacheInvalidate(name = “xxx“, key = “#results.![a+b]“,multi = true)是什么意思
  • React处理用户交互事件,如点击、输入框变化等,并使用事件处理函数来响应这些事件
  • 情侣游戏情侣飞行棋小程序系统:智能化互动,增添情感交流
  • 软考 系统架构设计师系列知识点之云计算(2)
  • Technology Strategy Patterns 学习笔记8- Communicating the Strategy-Decks(ppt模板)
  • Python实现WOA智能鲸鱼优化算法优化BP神经网络回归模型(BP神经网络回归算法)项目实战
  • 【机器学习基础】机器学习入门(2)
  • C#中在.NET 7.0控制台应用使用ADO.NET的方法
  • 垃圾回收(GC)
  • 【无标题】通用工作站设计方案:ORI-D3R600服务器-多路PCIe3.0的双CPU通用工作站
  • HarmonyOS元服务实现今天吃什么
  • 激活函数的本质
  • [工业自动化-18]:西门子S7-15xxx编程 - 软件编程 - PLC用于工业领域的嵌入式系统:硬件原理图、指令系统、系统软件架构、开发架构等
  • 【C++】——运算符重载
  • 怎么理解独立机器和虚拟机
  • 以太网和局域网
  • 【Git】第三篇:基本操作(配置本地仓库)