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

How to show a notification without a sound java

如何在构建时发出不发出声音的通知?我正在构建一个通知,但我的用户不喜欢它发出声音这一事实。

如何改成无声/无声?

我如何显示通知:

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.setSilent(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());

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

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

删除到 builder.setDefaults(Notification.DEFAULT_ALL); 的行。它不会播放声音,但如果愿意,您可能需要启用所有其他通知默认设置

如果您想要通知中的所有内容(声音、振动和灯光),请使用此选项。

builder.setDefaults(Notification.DEFAULT_ALL);

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

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

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

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

设置通知声音
Uri.parse("android.resource://"+YOUR_APP_PACKAGE_NAME+"/"+R.raw.silence) 

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

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

在 android 哦,对我来说它在通知中使用了这个设置:

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

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

NotificationManager.IMPORTANCE_LOW

代码如下:

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

它在 Android Oreo 中对我有用。

你应该这样写你的频道:

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

使用那个确切的代码:

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

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

我可能来晚了,但还是想补充一下。您可以使用 .setSound(null) on NotificationCompat.Builder builder 为所有低于 O.

的 OS 禁用声音

对于以上的 O 版本 n,在创建 NotificationChannel channel

之后添加 channel.setSound(null,null)

我上面的所有解决方案都已过时或仅涵盖某些 OS 个版本

您可以设置多个频道。 在我的例子中,我的应用程序有一个后台服务,有两个声音通知,一个没有声音的通知。我用这段代码得到它:

//创建频道:

    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

NotificationCompat.Builder.setSilent(true)

这与通知通道设置无关。这允许您拥有一个默认情况下发出声音的频道,但如果需要,您可以 post 静音通知而不会使整个频道静音。

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

我曾在 NotificationCompat.Builder 中使用过这段代码,试试这个,

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