【问题标题】:Disable notification sound on Android O在 Android O 上禁用通知声音
【发布时间】:2018-01-25 22:50:18
【问题描述】:

我尝试消除以下方法中的通知声音

我能够将它减少到只关闭一次,但它应该在 Android O 和​​更低版本中完全静音

我在 stackoverflow 和 google 上搜索了很长时间,但直到现在还没有完全奏效。

感谢任何帮助。

public void showUpdateProgressNotification(int id, String appName, int progress, String status, long downloadStart) {

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel
                    (NOTIFICATION_CHANNEL_ID, "Test Notifications", NotificationManager.IMPORTANCE_LOW);
            notificationChannel.setSound(null, null);

            // Configure the notification channel.
            notificationChannel.setDescription("Channel test");
            notificationManager.createNotificationChannel(notificationChannel);
        }

        Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
        cancelIntent.putExtra("id", id);
        PendingIntent cancel = PendingIntent.getBroadcast(this, ACTION_FILE_CANCEL.hashCode() + id,
                cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setContentTitle(appName)
                .setContentText(status)
                .setSmallIcon(android.R.drawable.stat_sys_download)
                .setDefaults(0)
                .setLargeIcon(BitmapFactory.decodeResource(MyApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
                .setProgress(100, progress, progress == 0)
                .setWhen(downloadStart)
                .setContentIntent(cancel)
                .setGroup(GROUP_KEY)
                .addAction(R.drawable.ic_close_black_24dp, "Cancel", cancel)
                .setColor(MyApplication_.getInstance().getResources().getColor(R.color.apps_color))
                .setOnlyAlertOnce(true);

        NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();
        inboxStyle.addLine(status);
        notification.setStyle(inboxStyle);

        notificationManager.notify(id, notification.build());

        addNotification(id);
 }

【问题讨论】:

  • 您需要卸载应用程序并重新安装它,因为一旦使用声音创建通知通道,它将不会被覆盖。因此,您需要卸载该应用并重新安装。

标签: android notifications android-8.0-oreo


【解决方案1】:

经过一番研究,我想通了。

这是最重要的部分:

//Configure the notification channel, NO SOUND
notificationChannel.setDescription("no sound");
notificationChannel.setSound(null,null); <---- ignore sound
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(false);

一开始执行此操作时,它仍然无法正常工作,但卸载我的应用并重新安装后一切都很好。

如果有人需要,这里是正确的代码:

public void showUpdateProgressNotification(int id, String appName, int progress, String status, long downloadStart) {

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(
                    NOTIFICATION_CHANNEL_ID, "My app no sound", NotificationManager.IMPORTANCE_LOW
            );

            //Configure the notification channel, NO SOUND
            notificationChannel.setDescription("no sound");
            notificationChannel.setSound(null,null);
            notificationChannel.enableLights(false);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.enableVibration(false);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
        cancelIntent.putExtra("id", id);
        PendingIntent cancel = PendingIntent.getBroadcast(this, ACTION_FILE_CANCEL.hashCode() + id,
                cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setContentTitle(appName)
                .setContentText(status)
                .setSmallIcon(android.R.drawable.stat_sys_download)
                .setDefaults(0)
                .setLargeIcon(BitmapFactory.decodeResource(MyApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
                .setProgress(100, progress, progress == 0)
                .setWhen(downloadStart)
                .setContentIntent(cancel)
                .setGroup(GROUP_KEY)
                .addAction(R.drawable.ic_close_black_24dp, "Cancel", cancel)
                .setColor(MyApplication_.getInstance().getResources().getColor(R.color.apps_color));

        NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();
        inboxStyle.addLine(status);
        notification.setStyle(inboxStyle);

        notificationManager.notify(id, notification.build());

        addNotification(id);
    }

【讨论】:

  • 应用了您的代码,即使卸载并重新安装,它仍然在我的设备中发出声音(我从 Firebase 控制台发送消息,启用声音选项)。
  • 当我只是复制并粘贴代码并运行时不起作用。但是在卸载应用程序并重新安装后它可以工作。谢谢..
  • !!重新安装应用程序工作!!
【解决方案2】:

使用以下方法始终让通知不发出声音,无论源频道设置如何。

NotificationCompat.Builder.setSilent(true)

? 这允许您拥有一个默认发出声音的频道,但允许您为个别频道发布静音通知,而不会使整个频道静音。

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

【讨论】:

    【解决方案3】:

    只需添加

    NotificationCompat.Builder.setNotificationSilent()
    

    这适用于所有 Android 版本,即使您设置了 PRIORITY_HIGH

    确保卸载应用并重新安装

    例子:

    val builder = NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentText("text")
                    .setContentTitle("content")
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setNotificationSilent() // just keep this line
                    .setSmallIcon(R.drawable.logo)
                    .setTicker("text"))
    
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    builder.setChannelId(CHANNEL_ID)
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 2017-12-18
      相关资源
      最近更新 更多