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;
    }

差不多就这样吧,自己摸索的一些东西,记录一下,希望能帮助到大家

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值