mirror of
https://github.com/IllusionMan1212/lyrics-grabbr.git
synced 2026-10-24 02:12:27 +00:00
WIP: make service foreground
This commit is contained in:
+11
-60
@@ -2,34 +2,11 @@ import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'notification_listener.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
||||
FlutterLocalNotificationsPlugin();
|
||||
|
||||
const spotifyPackage = "com.spotify.music";
|
||||
const notificationId = 0;
|
||||
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
const AndroidInitializationSettings initializationSettingsAndroid =
|
||||
AndroidInitializationSettings('@drawable/ic_notification');
|
||||
const InitializationSettings initializationSettings =
|
||||
InitializationSettings(android: initializationSettingsAndroid);
|
||||
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
|
||||
onSelectNotification: onNotificationClick);
|
||||
|
||||
runApp(const LyricsGrabbr());
|
||||
}
|
||||
|
||||
void onNotificationClick(String? payload) async {
|
||||
if (payload != null) {
|
||||
print('notification payload: $payload');
|
||||
}
|
||||
print('notification pressed');
|
||||
}
|
||||
|
||||
class LyricsGrabbr extends StatelessWidget {
|
||||
const LyricsGrabbr({Key? key}) : super(key: key);
|
||||
|
||||
@@ -135,39 +112,12 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
||||
}
|
||||
|
||||
void onData(NotificationEvent event) {
|
||||
if (event.packageName == spotifyPackage) {
|
||||
// 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.
|
||||
sendNotification(event);
|
||||
setState(() {
|
||||
_notifs.add(event);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> sendNotification(NotificationEvent notification) async {
|
||||
final AndroidNotificationDetails androidNotification =
|
||||
AndroidNotificationDetails(
|
||||
"Lyrics",
|
||||
"Lyrics",
|
||||
importance: Importance.low,
|
||||
priority: Priority.min,
|
||||
showWhen: false,
|
||||
ongoing: true,
|
||||
playSound: false,
|
||||
largeIcon: FilePathAndroidBitmap(notification.art),
|
||||
);
|
||||
|
||||
final NotificationDetails notificationDetails =
|
||||
NotificationDetails(android: androidNotification);
|
||||
|
||||
await flutterLocalNotificationsPlugin.show(
|
||||
notificationId,
|
||||
'Playing [${notification.songTitle}] By [${notification.artist}]',
|
||||
'Tap to get lyrics',
|
||||
notificationDetails,
|
||||
);
|
||||
// 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.
|
||||
setState(() {
|
||||
_notifs.add(event);
|
||||
});
|
||||
}
|
||||
|
||||
void startListening() {
|
||||
@@ -175,16 +125,17 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
||||
// that keeps track of which song is playing
|
||||
// and if pressed will fetch the lyrics and display them.
|
||||
|
||||
_notifications = AndroidNotificationListener();
|
||||
//_notifications = AndroidNotificationListener();
|
||||
|
||||
_subscription = _notifications.notificationStream.listen(onData);
|
||||
//_subscription = _notifications.notificationStream.listen(onData);
|
||||
AndroidNotificationListener.startService();
|
||||
setState(() => running = true);
|
||||
}
|
||||
|
||||
void stopListening() {
|
||||
setState(() => running = false);
|
||||
_subscription.cancel();
|
||||
flutterLocalNotificationsPlugin.cancel(notificationId);
|
||||
AndroidNotificationListener.stopService();
|
||||
//_subscription.cancel();
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -23,7 +23,6 @@ class NotificationEvent {
|
||||
final String artist;
|
||||
final String packageName;
|
||||
final String songTitle;
|
||||
final String art;
|
||||
final int playbackState;
|
||||
final int duration;
|
||||
|
||||
@@ -34,7 +33,6 @@ class NotificationEvent {
|
||||
required this.artist,
|
||||
required this.timeStamp,
|
||||
required this.songTitle,
|
||||
required this.art,
|
||||
required this.playbackState,
|
||||
required this.duration
|
||||
});
|
||||
@@ -44,7 +42,6 @@ class NotificationEvent {
|
||||
String name = map['packageName'];
|
||||
String message = map['packageMessage'];
|
||||
String text = map['packageText'];
|
||||
String icon = map['packageArt'];
|
||||
int playbackState = map['packagePlaybackState'];
|
||||
int duration = map['packageDuration'];
|
||||
|
||||
@@ -53,7 +50,6 @@ class NotificationEvent {
|
||||
artist: message,
|
||||
timeStamp: time,
|
||||
songTitle: text,
|
||||
art: icon,
|
||||
playbackState: playbackState,
|
||||
duration: duration
|
||||
);
|
||||
@@ -89,4 +85,12 @@ class AndroidNotificationListener {
|
||||
static Future<bool?> openPermissionSettings() async {
|
||||
return await _methodChannel.invokeMethod('plugin.openPermissionSettings');
|
||||
}
|
||||
|
||||
static Future<bool?> startService() async {
|
||||
return await _methodChannel.invokeMethod('plugin.startService');
|
||||
}
|
||||
|
||||
static Future<bool?> stopService() async {
|
||||
return await _methodChannel.invokeMethod('plugin.stopService');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user