在Android O上禁用通知声音

时间:2018-01-25 22:50:18

标签: android notifications android-8.0-oreo

我试图摆脱以下方法中的通知声音

我能够将其缩小为仅一次,但它应该在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);
 }

4 个答案:

答案 0 :(得分:26)

经过一些研究后,我发现了它。

这是至关重要的部分:

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

答案 1 :(得分: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)
            }

答案 2 :(得分:1)

一种更简单的方法是将通知频道的优先级设置为低:

    val chan = NotificationChannel(
        channelId,
        channelName, NotificationManager.IMPORTANCE_LOW
    )

答案 3 :(得分:1)

使用以下方法,确保无论通道设置如何,单个通知都不会发出声音

NotificationCompat.Builder.setSilent(true)

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

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