【问题标题】:NotificationManager.IMPORTANCE_HIGH still no soundNotificationManager.IMPORTANCE_HIGH 仍然没有声音
【发布时间】:2019-11-03 16:26:18
【问题描述】:

我正在尝试在 android 中创建通知,但无论我设置什么重要性和优先级,都没有声音。我假设声音(铃声)由 android 本身处理我不需要提供任何 mp3/wav 文件。我正在尝试使用 android 8.1(实际设备)、8.0(模拟器)和 8.1(模拟器)。在实际设备上创建的通知频道默认声音是关闭的,我不知道为什么,在模拟器上声音是打开的,但通知上仍然没有声音

这是我的代码:

public void sendMessage(View view) {
    createNotificationChannel();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle("New Notification")
            .setContentText("Lorem Ipsum")
            .setPriority(NotificationCompat.PRIORITY_HIGH);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

    notificationManager.notify(1, builder.build());
}

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "basic-channel";
        String description = "Lorem Ipsum";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

实际设备上的频道

模拟器上的频道

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    在构建通知时,需要指明要使用默认系统值:

    builder.setDefaults(Notification.DEFAULT_ALL)
    

    并记得清除应用数据或重新安装应用以正确重新创建通知通道。即使您在代码中重新创建通知通道,它也会保留其初始配置。

    【讨论】:

      【解决方案2】:

      根本原因: 对于实际设备,这是 OEM 的问题,在我的情况下是小米,我找到了这个链接threema.ch/en/faq/notification_channels_xiaomi,它说小米为所有应用程序设置了声音=关闭,除了少数像 FB、whatsApp 等。

      对于模拟器,我们需要完成设置过程,然后通知开始发出声音。

      【讨论】:

      • 谢谢——这对华为也有帮助;仅当我转到我的应用程序的设置 > 通知 > 通知类型 > 横幅并打开横幅时,才会显示提示通知
      【解决方案3】:

      看看这个:

      //Define sound URI
      Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                          .setSmallIcon(R.drawable.ic_launcher_foreground)
                  .setContentTitle("New Notification")
                  .setContentText("Lorem Ipsum")
                  .setPriority(NotificationCompat.PRIORITY_HIGH);
                  .setSound(soundUri); //This sets the sound to play
      

      【讨论】:

      • 在实际设备或模拟器上仍然没有声音
      • 不想播放自定义声音。我的实际设备是小米,我找到了这个链接threema.ch/en/faq/notification_channels_xiaomi,上面说小米为所有应用设置了声音=关闭,除了少数几个像 FB、whatsApp 等。现在我在考虑模拟器,他们会播放通知声音吗?跨度>
      • 它正在播放声音。检查此链接:stackoverflow.com/questions/53986070/…
      猜你喜欢
      • 2017-09-24
      • 1970-01-01
      • 2017-03-16
      • 2017-10-19
      • 2021-04-22
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多