我正在使用AlarmManager,当警报启动时,它还会显示通知。我已经为Oreo和Oreo之前创建了通知。在Oreo正常工作之前的通知-我可以禁用声音和设置灯光,但是无法在Oreo中实现此功能。我在振动方面也遇到了类似的问题,但能够找到可行的解决方案。我已经尝试了很多东西(123456 ...),从NotificationCompat到不断变化的重要性,但无法使其起作用。

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

这是我的代码,适用于奥利奥(Oreo)(不工作)和旧版(工作):

//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

最佳答案

终于找到了答案。每次更改与频道相关的任何内容时,都必须将频道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);
    }
}


希望这可以节省一些时间。

10-08 03:05