简体   繁体   English

通知声音在系统中静音

[英]Notification sound is muted in system

On the system, notifications are muted for my app.在系统上,我的应用程序的通知已静音。 How do I allow my app to play sound for notifications by default?默认情况下,如何允许我的应用为通知播放声音? It's on all phones它在所有手机上

Normally the following code controls the notification sound通常以下代码控制通知声音

NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "name", NotificationManager.IMPORTANCE_LOW);

Low importance means notifications are muted.低重要性意味着通知被静音。 High importance notifications notify the user.高重要性通知通知用户。

But if the Android System disables the sound for an application, you might not be able to play sound for the notification.但是,如果Android 系统禁用了应用程序的声音,则您可能无法为通知播放声音。

On pre Oreo devices, you can enable the notification sound by making the priority high在奥利奥之前的设备上,您可以通过将优先级设置为高来启用通知声音

  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setPriority(NotificationCompat.PRIORITY_HIGH) // Set priority to PRIORITY_HIGH to enable notification sound 
            .setContentIntent(pendingIntent)
            .setAutoCancel(true); 

On Oreo and above devices, you must create a channel for the notification在奥利奥及以上设备上,您必须为通知创建一个频道

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Set importance to IMPORTANCE_HIGH to enable notification sound on Android 8.0 and above
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "channel name", NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM