1

我正在使用通知渠道,作为标准,我想启用通知声音。我在 NotificationChannel 选项中设置了声音,但它被禁用了。我也尝试在 NotificationCompat.Builder 中设置音频,但这也没有奏效。

ps:不知道是不是我的手机问题,我有一个红米Note 7,安卓9,MIUI。

我已经在 Channel 选项 NotificationCompat.Builder 中尝试过,并且每次都单独尝试过,但它没有奏效。但是,如果我打开频道设置中的选项,它可以工作,但我不希望用户需要这样做。

    String CHANNEL_ID = "coin_master_item_generator";
    CharSequence CHANNEL_NAME = "Item Generator Coin Master";

    AudioAttributes audioAttributes = new AudioAttributes.Builder()
    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
    .build();


    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
    channel.setDescription("Notifies when Items are generated");
    channel.enableVibration(true);
    channel.setVibrationPattern(new long[]{2000,1000});
    channel.enableLights(true);
    channel.setLightColor(Color.WHITE);
    channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),audioAttributes);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);


    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setContentTitle("Item Generator")
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
        .setContentText("New Items generated");

    notificationManager.notify(1524,builder.build());
4

0 回答 0