首页 > 解决方案 > 无法为奥利奥通知启用闪烁灯并禁用通知声音

问题描述

我正在使用 AlarmManager,当警报启动时,它还会显示通知。我为奥利奥和奥利奥之前创建了通知。Oreo 之前的通知正常工作 - 我可以禁用声音和设置灯光,但我无法在 Oreo 中进行这项工作。我在振动方面遇到了类似的问题,但能够找到一个可行的解决方案。我已经尝试了很多东西(1、2、3、4、5、6 ... NotificationCompat改变重要性,但无法使其发挥作用。

我的问题仅在于奥利奥通知,我无法禁用声音(每次都会消失),也无法让灯光闪烁。我经历了一堆 SO 问题和官方文档。一些解决方案已过时,已弃用(NotificationCompat.Builder),其他解决方案根本不起作用(包括官方文档中的一些示例)。

这是我的奥利奥(不工作)和旧(工作)的代码:

//region Oreo notifications
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    CharSequence name = "AlarmNotification";
    String description = "Alarm notification";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;

    NotificationChannel mChannel = new NotificationChannel(channelIdOreo, name, importance);
    mChannel.setDescription(description);
    mChannel.setShowBadge(true);
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.RED);

    if (notificationManager != null) {
        notificationManager.createNotificationChannel(mChannel);
    }

    Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

    if (sNotifications.equals("false")) {
        //NOT WORKING
        mChannel.setSound(null, null);
    }
    //VIBRATION WORKING
    if (sVibration.equals("true")) {
        if (vibrator != null && vibrator.hasVibrator()) {
            VibrationEffect effect = VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE);
            vibrator.vibrate(effect);
        }
    }

    Notification notification = new Notification.Builder(context, channelIdOreo)
            .setContentTitle(contentTitleText)
            .setContentText(contentContentText)
            .setNumber(1)
            .setSmallIcon(whiteLogo)
            .setBadgeIconType(whiteLogo)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build();

    if (notificationManager != null) {
        notificationManager.notify(notificationCode, notification);
    }
}
//endregion

//region Pre-Oreo notifications
else {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
            .setSmallIcon(whiteLogo)
            .setLargeIcon(largeIcon)
            .setContentTitle(contentTitleText)
            .setContentText(contentContentText)
            .setOngoing(false)
            .setNumber(1)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setAutoCancel(true);

    mBuilder.setLights(colorPPDOrange, 1000, 2000);

    if (sNotifications.equals("true")) {
        mBuilder.setSound(uri);
    }
    if (sVibration.equals("true")) {
        mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    }
    mBuilder.setContentIntent(pendingIntent);
    if (notificationManager != null) {
        notificationManager.notify(notificationCode, mBuilder.build());
    }
}
//endregion

标签: javaandroidalarmmanagerandroid-notificationsandroid-8.0-oreo

解决方案


终于找到答案了。每次我更改与我的频道相关的任何内容时,我都必须将我的频道 ID 更改为一个全新的 ID,这是以前从未使用过的。它不能仅仅与当前的 id 不同。除此之外,我用代码中的实际字符串替换了我的字符串资源。我还必须移动几行代码,如下所示。

这就是我的代码现在的样子:

String channelIdOreo = "FfffffF";

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    //region Oreo otification
    Notification notification = new Notification.Builder(context, channelIdOreo)
            .setContentTitle(contentTitleText)
            .setContentText(contentContentText)
            .setNumber(1)
            .setSmallIcon(whiteLogo)
            .setBadgeIconType(whiteLogo)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build();
    //endregion

    NotificationChannel mChannel = new NotificationChannel(channelIdOreo, "Channel human readable title and stuff", NotificationManager.IMPORTANCE_DEFAULT);

    mChannel.enableLights(true);
    mChannel.setLightColor(Color.YELLOW);

    //region Conditions from settings
    if (sNotifications.equals("true")) {
        mChannel.setSound(uri, null);
    } else {
        mChannel.setSound(null, null);
    }

    if (sVibration.equals("true")) {
        mChannel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
    } else {
        mChannel.enableVibration(false);
    }
    //endregion

    if (notificationManager != null) {
        notificationManager.createNotificationChannel(mChannel);
    }
    if (notificationManager != null) {
        notificationManager.notify(notificationCode, notification);
    }
}

希望这会为某人节省一些时间。


推荐阅读