Disable sound from NotificationChannel

前端 未结 15 1269
时光说笑
时光说笑 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:27

    First you need to set

    importance = NotificationManager.IMPORTANCE_LOW;
    

    then

    Notification n = builder.setPriority(Notification.DEFAULT_ALL).build();
    n.defaults = 0;
    n.defaults |= Notification.DEFAULT_VIBRATE;
    
    0 讨论(0)
  • 2020-11-27 16:27

    NotificationManager.IMPORTANCE_LOW will work for sure

    0 讨论(0)
  • 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());
    
    0 讨论(0)
  • 2020-11-27 16:29
    NotificationManager.IMPORTANCE_LOW
    

    This will work but also collapsed your notification.

    0 讨论(0)
  • 2020-11-27 16:34

    As far as I have seen, since API 26 (Oreo) it is not possible to change the sound of a notification after it was once created.

        notificationManager.deleteNotificationChannel("channel_id"));
        NotificationChannel notificationChannel = new NotificationChannel(
            "channel_id", "channel_name",
            NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.setSound(null, null);
        notificationManager.createNotificationChannel(notificationChannel);
    

    Even deleting the channel before creation does not help.

    Google documentation says:

    android.app.NotificationManager public void deleteNotificationChannel(String channelId)

    Deletes the given notification channel. If you create a new channel with this same id, the deleted channel will be un-deleted with all of the same settings it had before it was deleted.

    NotificationChannel#setSound() documentation states

    Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel)

    Too bad that notificationBuilder.setSound(defaultSoundUri) does not work as well:

    This method was deprecated in API level 26. Use NotificationChannel#setSound(Uri, AudioAttributes) instead.

    Also using support library does not work. So sound is only settable once in the app and changing by the user is only possible in the settings of the notification. For me Ferran Negre's comment did not work. I do not understand why Google made this restriction. Too bad.

    0 讨论(0)
  • 2020-11-27 16:37
    NotificationManager.IMPORTANCE_LOW
    

    It produces no sound when notification is created as I need the same in my Music Application.

    And yes if you have already created a notification channel then either you need to change the channel id or simply uninstall the previous application and install again.

    0 讨论(0)
提交回复
热议问题