Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to support custom notification parameters? #1043

Open
gitbobobo opened this issue Sep 23, 2023 · 3 comments
Open

Is it possible to support custom notification parameters? #1043

gitbobobo opened this issue Sep 23, 2023 · 3 comments

Comments

@gitbobobo
Copy link

Feature proposal

In some Android systems, there is a feature for displaying lyrics in the status bar, such as Flyme OS.

image

To integrate this feature, you need to add two flags in the notification.flags and set the current lyrics to the ticker.

Motivating use case(s)

The link https://juejin.cn/post/7097415319643226125 is a tutorial I found about integrating status bar lyrics. I apologize that it's in Chinese, and you may need to use a translation tool.

In addition, I have also tried using flutter_local_notifications to send notifications, but it displays a notification in the notification bar at the same time. If I use the same ID as audio_service (1124), updating the status bar lyrics will replace audio_service's notification.

here is the code:

/// Flyme 状态栏歌词 TICKER 一直显示
const flagAlwaysShowTicker = 0x1000000;

/// 只更新 Flyme 状态栏歌词,不更新封面等
const flagOnlyUpdateTicker = 0x2000000;

class LocalNotification {
  static final FlutterLocalNotificationsPlugin instance =
      FlutterLocalNotificationsPlugin();

  static Future<void> init() async {
    const initializationSettingsAndroid =
        AndroidInitializationSettings('notify_icon');

    const initializationSettings =
        InitializationSettings(android: initializationSettingsAndroid);
    await instance.initialize(initializationSettings);

    // 如果开启了系统级状态栏歌词,则监听歌词变化
    if (AppSettings.player.enableSystemStatusBarLyric) {
      PlayerManager.lyricLineChangedStream.listen((event) {
        final currentAudio = PlayerManager.currentAudio;
        String title =
            CommonUtil.getSongTitle(currentAudio?.title, currentAudio?.artist);
        showLyric(event?.currentLine ?? title);
      });
    }
  }

  static Future<void> showLyric(String lyric) async {
    AndroidNotificationDetails androidNotificationDetails =
        AndroidNotificationDetails('channel_id', 'title',
            showWhen: false,
            channelDescription: 'description',
            playSound: false,
            ongoing: PlayerManager.getPlayer().isPlaying,
            additionalFlags: Int32List.fromList(
                [flagAlwaysShowTicker, flagOnlyUpdateTicker]),
            ticker: lyric);
    NotificationDetails notificationDetails =
        NotificationDetails(android: androidNotificationDetails);
    await instance.show(1124, null, null, notificationDetails);
  }
}

Therefore, the only solution is to attach these special parameters to the audio_service notification.

The branch https://github.com/gitbobobo/audio_service/tree/statusbar_lyric is a modification I made on top of audio_service. Its main function is to read the value of current_lyrics from mediaItem.extras and set it to the notification's ticker. Currently, it is not very generic and is for reference only.

@ryanheise
Copy link
Owner

That's interesting. Do you know if this is a standard flag across manufacturers or if there are also other flags by different manufactures that serve the same purpose? I'm asking since I may hard code the flag.

I consider this to be in a similar scope to copying media metadata across to the notification such as the current song title. So to implement this within audio_service, the data itself would be added to MediaItem, either as additional fields, or as extras. Then to update the ticker you would do mediaItem.add(mediaItem.value.copyWith(ticker: '...'));

This obviously updates the entire notification, but I don't think that should be too inefficient, and besides, to make such an app work on devices that don't support the ticker like this, you would probably fall back to updating the title/subtitle anyway which also works the same way. But then it seems like flagOnlyUpdateTicker would be unnecessary in this implementation. Am I understanding correctly?

@gitbobobo
Copy link
Author

Yes, in my app, I am currently using the mediaItem.add() method to update lyrics. As for updating lyrics to the title/subtitle as another user-selectable switch, here is my code for updating the status bar lyrics and notification lyrics:

PlayerManager.lyricLineChangedStream.listen((event) {
      if (PlayerManager.currentAudio == null || mediaItem.value == null) return;

      var displayTitle = CommonUtil.getSongTitle(
          PlayerManager.currentAudio!.title,
          PlayerManager.currentAudio!.artist);

      final lockLyric = AppSettings.player.enableLockLyric;
      final statusBarLyric = AppSettings.player.enableSystemStatusBarLyric;
      var newMediaItem = mediaItem.value!;
      if (lockLyric) {
        newMediaItem = newMediaItem.copyWith(
          title: event?.currentLine ?? PlayerManager.currentAudio!.title,
          artist: event?.currentLine == null
              ? PlayerManager.currentAudio!.artist
              : displayTitle,
        );
      } else {
        newMediaItem = newMediaItem.copyWith(
          title: PlayerManager.currentAudio!.title,
          artist: PlayerManager.currentAudio!.artist,
        );
      }
      if (statusBarLyric) {
        // 开启系统级状态栏歌词后,在extra中添加歌词信息
        newMediaItem = newMediaItem.copyWith(
          extras: {
            ...newMediaItem.extras!,
            'current_lyrics': event?.currentLine ?? displayTitle,
          },
        );
      } else {
        newMediaItem = newMediaItem.copyWith(
          extras: {
            ...newMediaItem.extras!,
            'current_lyrics': null,
          },
        );
      }
      mediaItem.add(newMediaItem);
    });

I tried removing the flagOnlyUpdateTicker flag, and the status bar lyrics, as well as the title/subtitle, can still be updated.

Currently, I know that Flyme OS and some quasi-native Android systems like Evolution-X support status bar lyrics. Whether it's a standard feature for different manufacturers is still something I need to wait for my users to try out over a period of time to see if it works.

@gitbobobo
Copy link
Author

Evolution-X system supports status bar lyrics, and after adding the flagAlwaysShowTicker flag, it can display status bar lyrics normally. So, I guess this flag might be universal.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants