title: Android: NotificationCompat.MediaStyle action buttons don't do anything
body: <p>I've got a simple Android app containing one <code>Activity</code> and a <code>Service</code> that derives from <code>MediaBrowserServiceCompat</code>. I've successfully gotten it set up to play audio from my main activity by using <code>MediaBrowserCompat</code> and <code>MediaControllerCompat</code>. It can even play and pause the audio from my Bluetooth headphones. All good.</p>
  
  <p>My challenge concerns the <code>NotificationCompat.MediaStyle</code> notification that appears on the lock screen and in the notifications tray. The notification appears properly. However, when I add buttons using <code>addAction()</code> and <code>MediaButtonReceiver.buildMediaButtonPendingIntent</code>, they don't do anything. If I instead add a dummy PendingIntent that just launches my main activity, that works fine.</p>
  
  <p>Here's my code to generate the notification (apologies, this is C# running in Xamarin, so the casing and names will be slightly different from what you might expect). This is inside my service class.</p>
  
  <pre><code>var builder = new NotificationCompat.Builder(this, CHANNEL_ID)
      .SetVisibility(NotificationCompat.VisibilityPublic)
      .SetSmallIcon(Resource.Drawable.ic_launcher)
      .SetContentTitle("Title")
      .SetContentText("Content")
      .SetSubText("Subtext")
      .SetLargeIcon(icon)
      .SetColor(Android.Graphics.Color.DarkOrange)
  
      .SetContentIntent(intent)
      .SetDeleteIntent(MediaButtonReceiver.BuildMediaButtonPendingIntent(this, PlaybackStateCompat.ActionStop))
  
      .AddAction(new NotificationCompat.Action(
          Resource.Drawable.ic_pause, "Pause",
          MediaButtonReceiver.BuildMediaButtonPendingIntent(this, PlaybackStateCompat.ActionPause)))
  
      .SetStyle(new Android.Support.V4.Media.App.NotificationCompat.MediaStyle()
          .SetShowActionsInCompactView(0)
          .SetMediaSession(this.mediaSession.SessionToken)
          .SetShowCancelButton(true)
          .SetCancelButtonIntent(MediaButtonReceiver.BuildMediaButtonPendingIntent(this, PlaybackStateCompat.ActionStop))
      );
  
  this.StartForeground(NOTIFICATION_ID, builder.Build());
  </code></pre>
  
  <p>Here's what I have looked at so far to try to solve this:</p>
  
  <ul>
  <li>When I start playback, I use <code>MediaSession.setActive(true)</code></li>
  <li>Each time I start and stop playback, I set the appropriate actions in <code>PlaybackStateCompat</code></li>
  <li>I have the session token set correctly.</li>
  <li>I do <strong>not</strong> have anything set up as a <code>MediaButtonReceiver</code> in my manifest, nor have I set anything up to handle <code>android.intent.action.MEDIA_BUTTON</code>, because I am targeting Android 5.0 and higher and using the <code>*Compat</code> classes, and my understanding is that that is no longer necessary.</li>
  </ul>
  
  <p>I know that media button events are being routed properly to my app, since my Bluetooth headphone buttons work. I tried it in my car and it works there too. <strong>It's just the buttons in the notification that won't work.</strong> I'm expecting them to generate calls to the appropriate methods of <code>MediaSessionCompat.Callback</code>. Is this incorrect? What am I doing wrong here?</p>
  
  <p>I would be grateful for any pointers.</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong>
  I got it working. I needed to add the following inside the <code>&lt;application&gt;</code> node of the manifest:</p>
  
  <pre><code>&lt;receiver android:name="android.support.v4.media.session.MediaButtonReceiver"&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.MEDIA_BUTTON" /&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p>...and the following inside the node of the Service that implements <code>MediaBrowserServiceCompat</code>:</p>
  
  <pre><code>&lt;intent-filter&gt;
      &lt;action android:name="android.media.browse.MediaBrowserService"/&gt;
  &lt;/intent-filter&gt;
  </code></pre>
  
  <p>I'm still a little confused about <em>why</em> this was necessary, since button presses from my Bluetooth headphones and car infotainment system were routed to the app just fine. More importantly, Google <a href="https://developer.android.com/reference/androidx/media/session/MediaButtonReceiver#buildMediaButtonPendingIntent(android.content.Context,%20long)" rel="noreferrer">says</a>:</p>
  
  <blockquote>
    <p>If you already have a <code>MediaBrowserServiceCompat</code> in your app,
    <code>MediaButtonReceiver</code> will deliver the received key events to the
    <code>MediaBrowserServiceCompat</code> by default. You can handle them in your
    <code>MediaSessionCompat.Callback</code>.</p>
  </blockquote>
  
  <p>They gave this as an <em>alternative</em> to the option "Service Handling ACTION_MEDIA_BUTTON," so I took that to mean I didn't need to do anything more with my manifest. If anyone could enlighten me here, I would appreciate it.</p>
  
  <p>But, for what it's worth, this worked for me.</p>