【问题标题】:How to show a notification without a sound javajava 如何在没有声音的情况下显示通知
【发布时间】:2017-02-10 02:31:28
【问题描述】:

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

如何将其更改为静音/完全没有声音?

我如何显示通知:

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

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

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

【问题讨论】:

  • @PavneetSingh 抱歉,没有想到“禁用”这个词
  • 让我大吃一惊,我不得不浪费时间研究如何获得无声通知。我试过这个(在 Xamarin 上)但没有运气:notificationBuilder.SetPriority(NotificationCompat.PriorityMin);
  • 好像你不能改变一个频道的优先级,所以我不得不创建两个频道,一个低,另一个默认。

标签: java android notifications android-notifications ringtone


【解决方案1】:

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

【讨论】:

  • 它会振动...我希望它完全没有反馈
  • 当我删除该行时,它只会显示通知:)。非常感谢
  • 如果您想保留构建器本身并只是来回更新声反馈怎么办?我在这篇文章中尝试了几个答案,但没有任何帮助: notificationCompatBuilderInstance!!.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY) .setGroupSummary(false) .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
  • AndroidX 中有一个新的 API 现在专门处理这种情况:stackoverflow.com/a/64811427/875834
【解决方案2】:

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

builder.setDefaults(Notification.DEFAULT_ALL);

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

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

如果您什么都不想要,请评论此行。

【讨论】:

    【解决方案3】:

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

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

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

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

    【讨论】:

      【解决方案4】:

      在 android O 中,对我来说,它在通知中使用此设置:

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

      【讨论】:

      • 对于 C#:Notification.Builder builder = new Notification.Builder(this) .SetSmallIcon(Resource.Drawable.btn_icon_header) .SetCustomContentView(contentView) .SetChannelId(URGENT_CHANNEL) .SetContentIntent(contentIntent) .SetGroupAlertBehavior (NotificationGroupAlertBehavior.Summary) .SetGroup("我的组") .SetGroupSummary(false);
      • 仅此一项让通知显示无声?这似乎不对,因为 DEFAULT_ALL 应该做声音、灯光和振动:developer.android.com/reference/android/app/…
      • @Mark 官方文档有时不是最新的,或者对于所有操作系统版本都不准确。我建议你自己试试代码。
      【解决方案5】:

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

      NotificationManager.IMPORTANCE_LOW
      

      代码如下:

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

      【讨论】:

      • 正如同一问题的不同线程中提到的:PRIORITY_LOW(Android 7.1 及更低版本无声音)IMPORTANCE_LOW(Android 8.0 及更高版本无声音)但最重要的部分是:一旦创建通道,除非应用程序被卸载,否则无法更改其优先级!!!
      • @kotoMJ 并删除频道并重新创建它也无济于事。
      【解决方案6】:

      它适用于 Android Oreo。

      你应该这样写你的频道:

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

      【讨论】:

      • 哇,我创建了一个空白 mp3,但看起来我可以在 NotificationChannel 上使用 .SetSound(null, null) ... 非常感谢! +1 也适用于Xamarin.Android
      • 这在不影响通知优先级的情况下完美运行。
      【解决方案7】:

      使用准确的代码:

      NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
      
      NOTIFICATION_CHANNEL_ID --> random String.
      channelName ==> random string
      

      【讨论】:

        【解决方案8】:

        我可能会迟到,但仍然想添加这个。您可以使用 .setSound(null) 上的 NotificationCompat.Builder builder 为所有低于 O 的操作系统禁用声音。

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

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

        【讨论】:

        • 对于 pre-O,您还可以在 builder 上调用 .setVibrate(null) 以防止其振动。
        【解决方案9】:

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

        //创建频道:

            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
        

        【讨论】:

          【解决方案10】:

          NotificationCompat.Builder.setSilent(true)

          无论通知通道设置如何,这都有效。这允许您拥有一个默认发出声音的频道,但允许您在需要时发布静音通知,而不会使整个频道静音。

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

          【讨论】:

          • 谢谢,我使用了 notificationBuilder.setNotificationSilent()
          • 没问题!仅供参考 notificationBuilder.setNotificationSilent() 已弃用。
          • 哇,所有这些令人费解的答案,你就得出了这个结论。谢谢!
          【解决方案11】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-05-23
            • 1970-01-01
            • 2012-06-13
            • 1970-01-01
            • 1970-01-01
            • 2014-10-11
            • 2021-10-05
            相关资源
            最近更新 更多