如何在没有声音的情况下显示通知

时间:2016-10-01 17:47:57

标签: java android notifications android-notifications ringtone

如何在制作时发出通知并发出声音?我正在制作通知,我的用户不喜欢发出声音的事实。

如何将其更改为无声/无声?

我如何显示通知:

android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(main);
builder.setStyle(new android.support.v7.app.NotificationCompat.BigTextStyle().bigText(text));
builder.setSmallIcon(R.drawable.app);
builder.setContentTitle("Rooster Maandag:");
builder.setOngoing(false);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager = (NotificationManager) main.getSystemService(main.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());

我试图在谷歌搜索,但我得到的唯一结果是如何播放声音,而不是如何播放声音......

修改 在某些人的眼中它可能是重复的,但在我看来,我找不到指定默认值的替代方法,而这种新方法称为setDefaults

11 个答案:

答案 0 :(得分:13)

要在OREO 8.1中禁用声音,请将通知的优先级更改为LOW,它将禁用通知声音:

NotificationManager.IMPORTANCE_LOW

代码类似于:

NotificationChannel chan1 = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_LOW);

答案 1 :(得分:11)

删除builder.setDefaults(Notification.DEFAULT_ALL);行。它不会播放声音,但如果首选

,您可能需要启用所有其他通知默认值

答案 2 :(得分:9)

在android O中,对我而言,它在通知中使用了这些设置:

                .setGroupAlertBehavior(GROUP_ALERT_SUMMARY)
                .setGroup("My group")
                .setGroupSummary(false)
                .setDefaults(NotificationCompat.DEFAULT_ALL)

答案 3 :(得分:7)

它在Android Oreo中对我有效。

您应该像这样编写您的频道:

NotificationChannel notificationChannel = new NotificationChannel("Id" , "Name", NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.setSound(null, null);
            notificationChannel.setShowBadge(false);
            notificationManager.createNotificationChannel(notificationChannel);

答案 4 :(得分:2)

使用此项如果您想要所有(声音,振动和灯光)通知。

builder.setDefaults(Notification.DEFAULT_ALL);

或者您可以根据您的要求启用或禁用项目。

builder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

如果你什么都不想,请评论这一行。

答案 5 :(得分:2)

我可能迟到了,但仍然想添加此内容。您可以在.setSound(null)上的NotificationCompat.Builder builder上为O以下的所有操作系统禁用声音。

对于上述O版本n,在创建channel.setSound(null,null)后添加NotificationChannel channel

我上面的所有解决方案要么已过时,要么仅涵盖某些操作系统版本

答案 6 :(得分:2)

NotificationCompat.Builder.setSilent(true)

无论通知通道设置如何,此方法均有效。这样一来,您就可以拥有一个默认情况下会发出声音的频道,但如果需要,您可以发布无声通知,而不会使整个频道保持静音。

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

答案 7 :(得分:1)

能够以任何优先级显示通知的简单方法是设置一些实际上是静音的声音。只需生成一些silence.mp3将其放入“raw”文件夹并使用Uri设置通知声音:

Uri.parse("android.resource://"+YOUR_APP_PACKAGE_NAME+"/"+R.raw.silence) 

您可以使用像Audacity这样的应用生成此.mp3。它有选项生成静音,只需设置多少秒,你就可以了。

如果您将默认值设置为0并将声音设置为空,则会在没有您听到通知的情况下显示通知,但您将无法显示具有更高优先级的通知。

答案 8 :(得分:1)

我在NotificationCompat.Builder中使用了这段代码,

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setNotificationSilent();

答案 9 :(得分:0)

使用该确切代码:

NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);

NOTIFICATION_CHANNEL_ID --> random String.
channelName ==> random string

答案 10 :(得分:0)

您可以设置多个频道。 就我而言,我的应用程序有一个后台服务,其中有两个声音通知,一个通知没有声音。我通过以下代码得到它:

//创建频道:

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

以及创建通知时:

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