setAndroidNotificationButtons method

void setAndroidNotificationButtons(
  1. List androidMediaButtons, {
  2. List<int>? androidCompactIndices,
})

Specify buttons for display in the Android notification.

androidMediaButtons is a List containing a mix of AndroidMediaButtonType and AndroidCustomMediaButton.

androidCompactIndices signifies which indices of androidMediaButtons should be shown in the 'compact' notification view. This has a max length of three.

Only supported on Android; no-op otherwise.

Implementation

void setAndroidNotificationButtons(List<dynamic> androidMediaButtons,
    {List<int>? androidCompactIndices}) async {
  const Map<AndroidMediaButtonType, String> androidMediaButtonTypeToString =
      <AndroidMediaButtonType, String>{
    AndroidMediaButtonType.stop: mediaStop,
    AndroidMediaButtonType.pause: mediaPause,
    AndroidMediaButtonType.play: mediaPlay,
    AndroidMediaButtonType.next: mediaNext,
    AndroidMediaButtonType.previous: mediaPrevious,
    AndroidMediaButtonType.seekForward: mediaSeekForward,
    AndroidMediaButtonType.seekBackward: mediaSeekBackward,
  };

  if (!Platform.isAndroid) return;

  try {
    final List<dynamic> androidMediaButtonsData =
        androidMediaButtons.map((dynamic buttonTypeOrCustomButton) {
      if (buttonTypeOrCustomButton is AndroidMediaButtonType) {
        final AndroidMediaButtonType buttonType = buttonTypeOrCustomButton;
        return androidMediaButtonTypeToString[buttonType];
      } else if (buttonTypeOrCustomButton is AndroidCustomMediaButton) {
        final AndroidCustomMediaButton customMediaButton =
            buttonTypeOrCustomButton;
        return <String, String>{
          mediaCustomTitleKey: customMediaButton.title,
          mediaCustomEventIdKey: customMediaButton.eventId,
          mediaCustomDrawableResourceKey: customMediaButton.drawableResource,
        };
      } else {
        _logger.severe('androidMediaButtons must only contain instances of '
            'AndroidMediaButtonType or AndroidCustomMediaButton');
        return null;
      }
    }).toList();

    await audioMethodChannel.invokeMethod<dynamic>(
        setAndroidMediaButtonsMethod, <String, dynamic>{
      mediaButtonsKey: androidMediaButtonsData,
      mediaCompactIndicesKey: androidCompactIndices
    });
  } on PlatformException catch (e) {
    _logger.severe('setAndroidMediaButtonsMethod error', e);
  }
}