diff --git a/packages/flutter_notification_center/example/lib/main.dart b/packages/flutter_notification_center/example/lib/main.dart index 60c78b7..eda350f 100644 --- a/packages/flutter_notification_center/example/lib/main.dart +++ b/packages/flutter_notification_center/example/lib/main.dart @@ -1,4 +1,5 @@ import 'package:example/custom_notification.dart'; +import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; @@ -6,7 +7,6 @@ import 'package:firebase_core/firebase_core.dart'; import 'package:flutter_notification_center_firebase/flutter_notification_center_firebase.dart'; import 'package:intl/date_symbol_data_local.dart'; import 'package:flutter_notification_center/flutter_notification_center.dart'; -import 'package:provider/provider.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); @@ -15,11 +15,8 @@ void main() async { await _signInUser(); runApp( - ChangeNotifierProvider( - create: (_) => FirebaseNotificationService(), - child: const MaterialApp( - home: NotificationCenterDemo(), - ), + const MaterialApp( + home: NotificationCenterDemo(), ), ); } @@ -42,7 +39,8 @@ Future _configureApp() async { } Future _signInUser() async { - //TO DO: Implement your own sign in logic + FirebaseAuth.instance.signInWithEmailAndPassword( + email: 'freek@iconica.nl', password: 'wachtwoord'); } class NotificationCenterDemo extends StatefulWidget { @@ -55,8 +53,16 @@ class NotificationCenterDemo extends StatefulWidget { class _NotificationCenterDemoState extends State { @override Widget build(BuildContext context) { + var service = FirebaseNotificationService( + newNotificationCallback: (notification) { + showDialog( + context: context, + builder: (context) => Dialog(child: Text(notification.title))); + debugPrint('New notification: ${notification.title}'); + }, + ); var config = NotificationConfig( - service: Provider.of(context), + service: service, notificationWidgetBuilder: (notification, context) => CustomNotificationWidget( notification: notification, @@ -75,7 +81,7 @@ class _NotificationCenterDemoState extends State { isReadDotColor: Colors.red, showNotificationIcon: true, ), - notificationService: Provider.of(context), + notificationService: service, notificationTranslations: const NotificationTranslations(), context: context, ), @@ -93,6 +99,18 @@ class _NotificationCenterDemoState extends State { ], ), body: const SizedBox.shrink(), + floatingActionButton: FloatingActionButton( + onPressed: () { + service.pushNotification( + NotificationModel( + id: UniqueKey().toString(), + title: 'Test', + body: 'This is a test', + scheduledFor: DateTime.now(), + ), + ); + }, + ), ); } } diff --git a/packages/flutter_notification_center/example/pubspec.yaml b/packages/flutter_notification_center/example/pubspec.yaml index de3c852..518eb91 100644 --- a/packages/flutter_notification_center/example/pubspec.yaml +++ b/packages/flutter_notification_center/example/pubspec.yaml @@ -24,7 +24,6 @@ dependencies: firebase_auth: ^4.2.6 firebase_core: ^2.5.0 firebase_storage: ^11.0.14 - provider: ^6.1.2 flutter_notification_center: path: ../ diff --git a/packages/flutter_notification_center/lib/src/services/notification_service.dart b/packages/flutter_notification_center/lib/src/services/notification_service.dart index 2a7e063..cd92147 100644 --- a/packages/flutter_notification_center/lib/src/services/notification_service.dart +++ b/packages/flutter_notification_center/lib/src/services/notification_service.dart @@ -26,7 +26,8 @@ abstract class NotificationService with ChangeNotifier { List listOfPlannedNotifications; /// Pushes a notification to the service. - Future pushNotification(NotificationModel notification); + Future pushNotification(NotificationModel notification, + [Function(NotificationModel model)? onNewNotification]); /// Retrieves the list of active notifications. Future> getActiveNotifications(); diff --git a/packages/flutter_notification_center_firebase/lib/src/services/firebase_notification_service.dart b/packages/flutter_notification_center_firebase/lib/src/services/firebase_notification_service.dart index 1350723..0a57eae 100644 --- a/packages/flutter_notification_center_firebase/lib/src/services/firebase_notification_service.dart +++ b/packages/flutter_notification_center_firebase/lib/src/services/firebase_notification_service.dart @@ -8,6 +8,8 @@ import '../config/firebase_collections.dart'; class FirebaseNotificationService with ChangeNotifier implements NotificationService { + final Function(NotificationModel) newNotificationCallback; + @override List listOfActiveNotifications; @override @@ -17,7 +19,8 @@ class FirebaseNotificationService late Timer _timer; FirebaseNotificationService( - {this.listOfActiveNotifications = const [], + {required this.newNotificationCallback, + this.listOfActiveNotifications = const [], this.listOfPlannedNotifications = const []}) { _startTimer(); } @@ -30,7 +33,8 @@ class FirebaseNotificationService } @override - Future pushNotification(NotificationModel notification) async { + Future pushNotification(NotificationModel notification, + [Function(NotificationModel model)? onNewNotification]) async { try { CollectionReference notifications = FirebaseFirestore.instance .collection(FirebaseCollectionNames.activeNotifications); @@ -41,6 +45,8 @@ class FirebaseNotificationService await notifications.doc(notification.id).set(notificationMap); listOfActiveNotifications.add(notification); + onNewNotification?.call(notification) ?? + newNotificationCallback(notification); notifyListeners(); } catch (e) { debugPrint('Error creating document: $e'); @@ -191,7 +197,8 @@ class FirebaseNotificationService for (NotificationModel notification in plannedNotifications) { if (notification.scheduledFor!.isBefore(currentTime) || notification.scheduledFor!.isAtSameMomentAs(currentTime)) { - await pushNotification(notification); + await pushNotification(notification, newNotificationCallback); + await deletePlannedNotification(notification); //Plan new recurring notification instance diff --git a/packages/flutter_notification_center_firebase/pubspec.yaml b/packages/flutter_notification_center_firebase/pubspec.yaml index 570d640..bcca28b 100644 --- a/packages/flutter_notification_center_firebase/pubspec.yaml +++ b/packages/flutter_notification_center_firebase/pubspec.yaml @@ -21,7 +21,6 @@ dependencies: firebase_storage: ^11.0.14 cupertino_icons: ^1.0.2 - provider: ^6.1.2 flutter_notification_center: path: ../flutter_notification_center