Android - Is there a way to enable the ringer/notification sounds only when using headphones?

I don't have Android Marshmallow but following apps can easily achieve the goal on Android Lollipop 5.1. Should not fail on your OS, I believe. Let me know the result in any case.

MacroDroid

When headphones are plugged:

  • Trigger: Headphones Insert/Remove → Headphones Inserted
  • Action: Volume Change → select Notification and Ringer, and configure the slider for the level of volume you want

When headphones are removed:

  • Trigger: Headphones Insert/Remove → Headphones Removed
  • Action: Volume Change → select Notification and Ringer, and keep the slider to 0

Automate

It seems to work too. Your flow should look like this:

(Click image to enlarge)

IMG:

Relevant blocks:

  • Interface → Wired headset plugged?
  • Settings → Ringer mode set

The said flow would cause the ringer and notification volume to come back to the level you set them to, before switching the phone to vibration.

Atooma

It worked as well. However, it appears to not been updated since May 2015.

When headphones are connected:

  • IF: Headphone → Plugged
  • DO: Audio Manager → Set ringer volume → configure the slider
  • DO: Audio Manager → Notification volume → configure the slider

When headphones are removed:

  • IF: Headphone → Unplugged
  • DO: Silent mode → On → Vibrate: Yes

Droid Automation

It also worked well for me.

Instructions:

  1. Profile: Headset plugged

    1. Trigger: Headset state → Becomes plugged
    2. Task: Sounds & Media → Volume: Ringtones → configure
    3. Task: Sounds & Media → Volume: Notifications → configure
  2. Profile: Headset unplugged

    1. Trigger: Headset state → Becomes unplugged
    2. Task: Sounds & Media → Volume: Ringtones → configure to zero

Ensure Tasker is installed and enabled. I tested this with Tasker 4.8 on Android 6.0.1 Marshmallow. If you are using Tasker on Marshmallow, it will work more effectively if you disable battery optimisation under the Android Settings (although this may also drain the battery quicker). To do this, navigate to Battery -> Triple dot menu (in the upper-right hand corner) -> Battery optimisation. Then click on the drop-down menu and select All apps and scroll down to Tasker. Click on Tasker and press Don't optimise followed by Done.

To do this, open Tasker and create a new task. Name it whatever you like. Then add an action and navigate to Audio -> Ringer Volume. Drag the slider to 7 (this is the maximum volume and you can adjust this if you want). Do the same for Audio -> Notifcation Volume under the same task.

Now add a profile and navigate to State -> Hardware -> Headset Plugged. Don't change the default options here and navigate back. You will now be prompted to add a task. Select the task that you created earlier.

Once ensuring that the profile is enabled, exit Tasker and set your notifcation/ringer volume to vibrate.

That's it. The ringer and notification volumes should now change automatically when you plug and unplug your headphones.