通知声音在系统中静音

时间:2021-07-02 07:52:48

标签: java android

在系统上,我的应用的通知已静音。默认情况下,如何允许我的应用为通知播放声音?It's on all phones

2 个答案:

答案 0 :(得分:0)

通常以下代码控制通知声音

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

低重要性意味着通知被静音。高重要性通知通知用户。

但如果 Android 系统禁用了应用程序的声音,您可能无法为通知播放声音。

答案 1 :(得分:0)

在 Oreo 之前的设备上,您可以通过将优先级设置为高来启用通知声音

  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); 

在奥利奥及以上设备上,您必须为通知创建一个频道

    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);
    }