Disable sound from NotificationChannel

前端 未结 15 1331
时光说笑
时光说笑 2020-11-27 15:50

Today I started targeting API 26 which forced me to use Notification Channels.

My problem is that now on each new notification (including updates to it) an annoying

15条回答
  •  情歌与酒
    2020-11-27 16:28

    No need to use .setSound(null, null)

    just use below code

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,
                    getString(R.string.notification_channel_id))
                    .setContentTitle("Audio Recording")
                    .setContentText("recording in progress")
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setSmallIcon(R.mipmap.ic_launcher);
    
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(
                        getString(R.string.notification_channel_id), "AOD+", NotificationManager.IMPORTANCE_LOW
                );
                channel.setDescription("Call Recorder is running");
                channel.setShowBadge(true);
                channel.canShowBadge();
                channel.setLightColor(Color.GREEN);
                channel.enableVibration(false);
    
                assert notificationManager != null;
                notificationManager.createNotificationChannel(channel);
            }
    
            assert notificationManager != null;
            startForeground(256/* must be greater than 0*/, notificationBuilder.build()); //I am using this for audio recording
            //notificationManager.notify(0, notificationBuilder.build());
    

提交回复
热议问题