How to show a notification without a sound java

后端 未结 11 1701
鱼传尺愫
鱼传尺愫 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:33

    You can set more than one channel. In my case, my aplication has a background service with two sounds notifications, and one notification without sound. I get it with this code:

    //creating channels:

        NotificationChannel channel1 = new NotificationChannel(CHANNEL_ID_1, "CarNotification1", NotificationManager.IMPORTANCE_HIGH);
        NotificationChannel channel2 = new NotificationChannel(CHANNEL_ID_2, "CarNotification2", NotificationManager.IMPORTANCE_MIN);
        NotificationChannel channel3 = new NotificationChannel(CHANNEL_ID_3, "CarNotification3", NotificationManager.IMPORTANCE_HIGH);
    
        //mSound1 and mSound2 are Uri
        channel1.setSound(mSound1, null);
        channel3.setSound(mSound2, null);
    

    and when I create the notification:

    String channelId;
    switch (situation){
        case situation2:
            channelId=CHANNEL_ID_2;
            break;
        case situation1:
            channelId=CHANNEL_ID_1;
            break;
        default:
            channelId=CHANNEL_ID_3;
    }
    
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
    //etcetera
    

提交回复
热议问题