How to show a notification without a sound java

后端 未结 11 1679
鱼传尺愫
鱼传尺愫 2020-12-11 15:09

How can I make a notification that doesn\'t make a sound when I build it? I am building a notification, and my users don\'t like the fact that it makes a sound.

相关标签:
11条回答
  • 2020-12-11 15:39

    I might be late but still wants to add this . You can disable sound using .setSound(null) on NotificationCompat.Builder builder for all OS below O.

    For O version n above add channel.setSound(null,null) after creating NotificationChannel channel

    All the solutions above mine is either outdated or covers some OS versions only

    0 讨论(0)
  • 2020-12-11 15:41

    use that exact code:

    NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
    
    NOTIFICATION_CHANNEL_ID --> random String.
    channelName ==> random string
    
    0 讨论(0)
  • 2020-12-11 15:42

    NotificationCompat.Builder.setSilent(true)

    This works regardless of the Notification Channel setting. This allows you to have a channel that makes sound by default but allows you to post silent notifications if desired without making the entire channel silent.

    Reference: https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setSilent(boolean)

    0 讨论(0)
  • 2020-12-11 15:45

    To disable the sound in OREO 8.1, change the priority of the notification as LOW and it will disable the sound of notification:

    NotificationManager.IMPORTANCE_LOW
    

    The code is like:

    NotificationChannel chan1 = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_LOW);
    
    0 讨论(0)
  • 2020-12-11 15:47

    Remove the line to builder.setDefaults(Notification.DEFAULT_ALL);. It will not play the sound, but you may need to enable all other notification defaults if preferred

    0 讨论(0)
提交回复
热议问题