Android8.0 NotificationChannel修改铃声和振动的坑
记录一下工作中遇到的一些坑,都是一些网上没有找到解决办法自己摸索出来的一些东西。希望让有需要的人少踩一些坑
Android8.0 Google推出了NotificationChannel渠道的概念,对于NotificationChannel我们可以通过以下方式来设置铃声和振动。
NotificationChannel channel = new NotificationChannel(newChannelId, BaseApplication.getAppString(R.string.push_channel_name), NotificationManager.IMPORTANCE_HIGH);if (!TextUtils.isEmpty(uri)) {Logger.i(TAG, "8.0通知铃声:" + uri);Uri mUri = Uri.parse(uri);channel.setSound(mUri, Notification.AUDIO_ATTRIBUTES_DEFAULT);} else {channel.setSound(null, null);}Logger.i(TAG, "8.0是否开启振动:" + vibrateEnable);channel.enableLights(true);if (vibrateEnable) {channel.enableVibration(true);channel.setVibrationPattern(new long[]{100, 200, 300});} else {channel.enableVibration(false);channel.setVibrationPattern(new long[]{0});}nm.createNotificationChannel(channel);
那么如何的修改铃声和振动呢?最初天真的我以为重新调用NotificationChannel.setSound()方法来修改铃声,最终结果大家都清楚,是不会生效的,看了google的文档有这样一句话
意思是只能在create一个渠道之前修改铃声,在创建之后不支持修改。没办法,只能去重新创建一个渠道设置铃声振动。对于之前创建的渠道,你必须还得去通过deleteNotificationChannel(String channelId)去删除。但是这里又有另外一个坑。你什么时候去删除呢?第一次测试我是在修改铃声或者振动的时候创建一个新的渠道,把之前所有旧的渠道都删除,但是这样会有一个bug,之前渠道上还在状态栏显示的Notification都会删除掉,所有要做一个判断,如果当前渠道在状态栏没有notification显示则删除,否则继续保存,代码如下:
private static void deleteNoNumberNotification(NotificationManager nm, String newChannelId) {List<NotificationChannel> notificationChannels = nm.getNotificationChannels();if (Utils.isEmpty(notificationChannels) || notificationChannels.size() < 5) {return;}for (NotificationChannel channel : notificationChannels) {if (channel.getId() == null || channel.getId().equals(newChannelId)) {continue;}int notificationNumbers = getNotificationNumbers(nm, channel.getId());Logger.i(TAG, "notificationNumbers: " + notificationNumbers + " channelId:" + channel.getId());if (notificationNumbers == 0) {Log.i(TAG, "deleteNoNumberNotification: " + channel.getId());nm.deleteNotificationChannel(channel.getId());}}}/*** 获取某个渠道下状态栏上通知显示个数** @param mNotificationManager NotificationManager* @param channelId String* @return int*/@RequiresApi(api = Build.VERSION_CODES.O)private static int getNotificationNumbers(NotificationManager mNotificationManager, String channelId) {if (mNotificationManager == null || TextUtils.isEmpty(channelId)) {return -1;}int numbers = 0;StatusBarNotification[] activeNotifications = mNotificationManager.getActiveNotifications();for (StatusBarNotification item : activeNotifications) {Notification notification = item.getNotification();if (notification != null) {if (channelId.equals(notification.getChannelId())) {numbers++;}}}return numbers;}
差不多就这样吧,自己摸索的一些东西,记录一下,希望能帮助到大家
Android8.0 NotificationChannel修改铃声和振动的坑相关推荐
- Android8.0、9.0、10.0、11.0的新特性兼容适配代码修改
<Android Studio开发实战 从零基础到App上线(第2版)>在书后面的附录中给出了Android8和Android9的主要特性说明,附录表格如下图所示: 不过附录表格只涵盖了常 ...
- Android8.0通知功能的实现方法
首先,对于Android8.0以上版本系统的通知功能,仅仅靠NoticeficationCompat.Builder已经不能实现了,因为8.0以后引入了一个通道的概念---NotificationCh ...
- android8camera,[Android8.0/9.0/10]Camera:外接 USB 摄像头
概述 Android 平台支持使用即插即用的 USB 摄像头(即网络摄像头),但前提是这些摄像头采用标准的 Android Camera2 API 和摄像头 HIDL 接口.网络摄像头通常支持 USB ...
- Android之解决Android8.0手机(Notification)收不到自定义消息通知以及其它手机得到数据不同步
1 问题 app,自定义消息通知的时候,在Android8.0手机上收不到通知 2 解决办法 NotificationManager需要创建NotificationChannel,然后调用create ...
- android 打印kernel log,android8.0 kernel4.9.44 各层log打开
一.问题 平台android8.0 kernel4.9.44,想加个i2c驱动,打开log都是一件脑瓜疼的事. 二.log总结 android有三种log. 1.kernel下面的,就是driver层 ...
- android 跳转动画黑屏,Android8.0 适配解决页面跳转过程出现短暂黑屏的问题
小菜在适配 Android8.0 过程中,遇到很多问题,有很多很常见的问题,今天来整理一下页面跳转时黑屏的问题. 问题猜测 猜测一: 显示 Activity 页面之前会优先开启一个 Starting ...
- Android8.0及以上系统 WiFi热点 版本适配
代码地址如下: http://www.demodashi.com/demo/13907.html ###一.准备工作 开发环境: jdk1.8 AS(3.0.1) 运行环境: 华为V10(And ...
- CTS(5)---Android8.0中CTS测试对于TEE的要求
Android8.0中CTS测试对于TEE的要求 我们知道TEE在Google Android7.0后成为一种强制性要求,那么Android8.0又是如何要求的呢? 对于Android8.0中CTS测 ...
- Android 8.0 学习(14)---Android8.0适配分析
公司项目Android8.0适配分析 1. 官方推荐的兼容测试方式 2. Android8.0行为变更说明 Android 8.0 行为变更包括两个部分:针对所有 API 级别的应用和针对 And ...
最新文章
- COM 组件设计与应用(十一)—— IDispatch 及双接口的调用
- Recurrent Neural Networks Tutorial, Part 1 – Introduction to RNNs
- Navicat for MySQL 连接 Mysql 8.0.11 出现1251- Client does not support authentication protocol 错误
- 动手为王 | Oracle 数据库跨版本升级迁移实践
- 监听者模式 java_java监听者模式
- linux查看创建目录命令,Linux菜鸟——常见命令一 查看及创建目录文件等命令
- 让一个.sh文件一直运行_想做AI鉴黄工具?这有一个内含20多万张“不可描述”图片的数据集...
- 山东联通中兴B860 AV1.1机顶盒 刷机问题解决
- 家庭局域网_KODI超强的局域网视频播放软件
- 缓冲器(跟随器)电路
- 如何使用摸瓜快速查手机apk加壳
- Segger Embedded Studio使用有什么技巧?
- pycharm 常用快捷键(中英文对照表)
- 单机传奇找不到登陆器服务器列表,如果传奇服务端里面没有带登陆器怎么办?...
- Android屏幕适配很难嘛?其实也就那么回事,吐血整理
- 命题逻辑完备性定理证明
- 北邮计算机学院视频,傅慧源(博导)
- 输入3×4的矩阵 将值为负的位置和值输出
- 人事管理系统哪个好用?人事管理系统推荐!
- A. Arena of Greed