在Android O上禁用通知声音

7nbnzgx9  于 2022-12-02  发布在  Android
关注(0)|答案(4)|浏览(205)

我在下面尝试摆脱通知音的方法。
我能够 * 减少它只去了一次 *,但它应该是完全沉默在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);
 }
carvr3hs

carvr3hs1#

我又研究了一番才弄明白。
这是至关重要的部分:

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

5w9g7ksd2#

使用以下命令始终发出通知,无论源通道设置如何,都不发出声音。
NotificationCompat.Builder.setSilent(true)
💡 这允许您有一个默认情况下发出声音的通道,但允许您为单个通道发布静音通知,而无需使整个通道静音。
参考:https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setSilent(布尔值)

hiz5n14c

hiz5n14c3#

只需添加

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)
            }
gojuced7

gojuced74#

在您的通知通道内刚刚设置

notificationChannel.setSound(null, null);

相关问题