mirror of
https://github.com/IllusionMan1212/lyrics-grabbr.git
synced 2026-10-24 02:12:27 +00:00
feat: get duration, playback state and album art from notification
This commit is contained in:
+10
-12
@@ -13,8 +13,7 @@ const notificationId = 0;
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// TODO: do the preconfiguration for the android notifications.
|
||||
// e.g: notification icon
|
||||
// TODO: change the default flutter icon to my own.
|
||||
const AndroidInitializationSettings initializationSettingsAndroid =
|
||||
AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||
const InitializationSettings initializationSettings =
|
||||
@@ -143,17 +142,15 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
||||
// TODO: if the app is in the background, only update our notif
|
||||
// if the app is in the foreground, update the notif, and
|
||||
// fetch the new lyrics and display them.
|
||||
var song = event.packageText;
|
||||
var artist = event.packageMessage;
|
||||
sendNotification(song, artist);
|
||||
sendNotification(event);
|
||||
setState(() {
|
||||
_notifs.add(event);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> sendNotification(String title, String body) async {
|
||||
const AndroidNotificationDetails androidNotification =
|
||||
Future<void> sendNotification(NotificationEvent notification) async {
|
||||
final AndroidNotificationDetails androidNotification =
|
||||
AndroidNotificationDetails(
|
||||
"Lyrics",
|
||||
"Lyrics",
|
||||
@@ -162,15 +159,16 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
||||
showWhen: false,
|
||||
ongoing: true,
|
||||
playSound: false,
|
||||
largeIcon: ByteArrayAndroidBitmap.fromBase64String(notification.art),
|
||||
);
|
||||
|
||||
const NotificationDetails notificationDetails =
|
||||
final NotificationDetails notificationDetails =
|
||||
NotificationDetails(android: androidNotification);
|
||||
|
||||
await flutterLocalNotificationsPlugin.show(
|
||||
notificationId,
|
||||
title,
|
||||
body,
|
||||
'${notification.songTitle} - ${notification.artist}',
|
||||
'Tap to get lyrics',
|
||||
notificationDetails,
|
||||
);
|
||||
}
|
||||
@@ -204,8 +202,8 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
||||
itemBuilder: (BuildContext context, int idx) {
|
||||
final entry = _notifs[idx];
|
||||
return ListTile(
|
||||
leading: Text(entry.packageText),
|
||||
trailing: Text(entry.packageMessage),
|
||||
leading: Text(entry.songTitle),
|
||||
trailing: Text(entry.artist),
|
||||
);
|
||||
}),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
|
||||
@@ -13,19 +13,32 @@ class NotificationException implements Exception {
|
||||
}
|
||||
}
|
||||
|
||||
class NotificationEvent {
|
||||
String packageMessage;
|
||||
String packageName;
|
||||
String packageExtra;
|
||||
String packageText;
|
||||
DateTime timeStamp;
|
||||
abstract class PlaybackState {
|
||||
static const STATE_INVALID = -1;
|
||||
static const STATE_PAUSED = 2;
|
||||
static const STATE_PLAYING = 3;
|
||||
}
|
||||
|
||||
NotificationEvent({
|
||||
class NotificationEvent {
|
||||
final String artist;
|
||||
final String packageName;
|
||||
final String packageExtra;
|
||||
final String songTitle;
|
||||
final String art;
|
||||
final int playbackState;
|
||||
final int duration;
|
||||
|
||||
final DateTime timeStamp;
|
||||
|
||||
const NotificationEvent({
|
||||
required this.packageName,
|
||||
required this.packageMessage,
|
||||
required this.artist,
|
||||
required this.timeStamp,
|
||||
required this.packageExtra,
|
||||
required this.packageText
|
||||
required this.songTitle,
|
||||
required this.art,
|
||||
required this.playbackState,
|
||||
required this.duration
|
||||
});
|
||||
|
||||
factory NotificationEvent.fromMap(Map<dynamic, dynamic> map) {
|
||||
@@ -34,13 +47,20 @@ class NotificationEvent {
|
||||
String message = map['packageMessage'];
|
||||
String text = map['packageText'];
|
||||
String extra = map['packageExtra'];
|
||||
String icon = map['packageArt'];
|
||||
int playbackState = map['packagePlaybackState'];
|
||||
int duration = map['packageDuration'];
|
||||
|
||||
return NotificationEvent(packageName: name, packageMessage: message, timeStamp: time,packageText: text , packageExtra: extra);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return "Notification Event \n Package Name: $packageName \n - Timestamp: $timeStamp \n - Package Message: $packageMessage";
|
||||
return NotificationEvent(
|
||||
packageName: name,
|
||||
artist: message,
|
||||
timeStamp: time,
|
||||
songTitle: text,
|
||||
packageExtra: extra,
|
||||
art: icon,
|
||||
playbackState: playbackState,
|
||||
duration: duration
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user