即使我使用notificationChannel.setSound(null,null),通知也不是沉默的;

问题描述 投票:1回答:1

不知怎的,我的通知仍然发出声音。我做错了什么?

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // For foreground service
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        // Creating channel for notification
        String id = BackroundService.class.getSimpleName();
        String name = BackroundService.class.getSimpleName();
        NotificationChannel notificationChannel = new NotificationChannel(id,
                name, NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        service.createNotificationChannel(notificationChannel);
        notificationChannel.setSound(null, null);

        // Foreground notification
        Notification notification = new Notification.Builder(this, id)
                .setContentTitle(getText(R.string.app_name))
                .setContentText("KSL is protecting you!")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .setTicker("Ticker text")
                .build();

        startForeground(9, notification);
    }

我使用了notificationChannel.setSound(null, null);方法,但它不起作用。

android notifications
1个回答
0
投票

将您的频道ID提供给您的通知。

Notification notification = new Notification.Builder(this, id)
            .setContentTitle(getText(R.string.app_name))
            .setContentText("KSL is protecting you!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent)
            .setTicker("Ticker text")
            .setChannelId(channelId)
            .build();

编辑

确保卸载并重新安装应用程序以使更改生效。

编辑2

NotificationChannel notificationChannel = new NotificationChannel(id,
            name, NotificationManager.IMPORTANCE_LOW);

Android系统将通知重要性设置为NotificationManager.IMPORTANCE_HIGH(默认情况下会播放声音),即使您重视NotificationManager.IMPORTANCE_DEFAULT(默认情况下这不播放声音)。我不知道为什么会这样开心,但为了解决您的问题,我建议将重要的更改为NotificationManager.IMPORTANCE_LOW。

© www.soinside.com 2019 - 2024. All rights reserved.