fix: change bit of code to make popups work

This commit is contained in:
Niels Gorter 2024-04-12 15:16:39 +02:00
parent b5dc30bbf2
commit f9e7728412
5 changed files with 39 additions and 15 deletions

View file

@ -1,4 +1,5 @@
import 'package:example/custom_notification.dart'; import 'package:example/custom_notification.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_dotenv/flutter_dotenv.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:flutter_notification_center_firebase/flutter_notification_center_firebase.dart';
import 'package:intl/date_symbol_data_local.dart'; import 'package:intl/date_symbol_data_local.dart';
import 'package:flutter_notification_center/flutter_notification_center.dart'; import 'package:flutter_notification_center/flutter_notification_center.dart';
import 'package:provider/provider.dart';
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
@ -15,11 +15,8 @@ void main() async {
await _signInUser(); await _signInUser();
runApp( runApp(
ChangeNotifierProvider( const MaterialApp(
create: (_) => FirebaseNotificationService(), home: NotificationCenterDemo(),
child: const MaterialApp(
home: NotificationCenterDemo(),
),
), ),
); );
} }
@ -42,7 +39,8 @@ Future<void> _configureApp() async {
} }
Future<void> _signInUser() async { Future<void> _signInUser() async {
//TO DO: Implement your own sign in logic FirebaseAuth.instance.signInWithEmailAndPassword(
email: 'freek@iconica.nl', password: 'wachtwoord');
} }
class NotificationCenterDemo extends StatefulWidget { class NotificationCenterDemo extends StatefulWidget {
@ -55,8 +53,16 @@ class NotificationCenterDemo extends StatefulWidget {
class _NotificationCenterDemoState extends State<NotificationCenterDemo> { class _NotificationCenterDemoState extends State<NotificationCenterDemo> {
@override @override
Widget build(BuildContext context) { 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( var config = NotificationConfig(
service: Provider.of<FirebaseNotificationService>(context), service: service,
notificationWidgetBuilder: (notification, context) => notificationWidgetBuilder: (notification, context) =>
CustomNotificationWidget( CustomNotificationWidget(
notification: notification, notification: notification,
@ -75,7 +81,7 @@ class _NotificationCenterDemoState extends State<NotificationCenterDemo> {
isReadDotColor: Colors.red, isReadDotColor: Colors.red,
showNotificationIcon: true, showNotificationIcon: true,
), ),
notificationService: Provider.of<FirebaseNotificationService>(context), notificationService: service,
notificationTranslations: const NotificationTranslations(), notificationTranslations: const NotificationTranslations(),
context: context, context: context,
), ),
@ -93,6 +99,18 @@ class _NotificationCenterDemoState extends State<NotificationCenterDemo> {
], ],
), ),
body: const SizedBox.shrink(), body: const SizedBox.shrink(),
floatingActionButton: FloatingActionButton(
onPressed: () {
service.pushNotification(
NotificationModel(
id: UniqueKey().toString(),
title: 'Test',
body: 'This is a test',
scheduledFor: DateTime.now(),
),
);
},
),
); );
} }
} }

View file

@ -24,7 +24,6 @@ dependencies:
firebase_auth: ^4.2.6 firebase_auth: ^4.2.6
firebase_core: ^2.5.0 firebase_core: ^2.5.0
firebase_storage: ^11.0.14 firebase_storage: ^11.0.14
provider: ^6.1.2
flutter_notification_center: flutter_notification_center:
path: ../ path: ../

View file

@ -26,7 +26,8 @@ abstract class NotificationService with ChangeNotifier {
List<NotificationModel> listOfPlannedNotifications; List<NotificationModel> listOfPlannedNotifications;
/// Pushes a notification to the service. /// Pushes a notification to the service.
Future pushNotification(NotificationModel notification); Future pushNotification(NotificationModel notification,
[Function(NotificationModel model)? onNewNotification]);
/// Retrieves the list of active notifications. /// Retrieves the list of active notifications.
Future<List<NotificationModel>> getActiveNotifications(); Future<List<NotificationModel>> getActiveNotifications();

View file

@ -8,6 +8,8 @@ import '../config/firebase_collections.dart';
class FirebaseNotificationService class FirebaseNotificationService
with ChangeNotifier with ChangeNotifier
implements NotificationService { implements NotificationService {
final Function(NotificationModel) newNotificationCallback;
@override @override
List<NotificationModel> listOfActiveNotifications; List<NotificationModel> listOfActiveNotifications;
@override @override
@ -17,7 +19,8 @@ class FirebaseNotificationService
late Timer _timer; late Timer _timer;
FirebaseNotificationService( FirebaseNotificationService(
{this.listOfActiveNotifications = const [], {required this.newNotificationCallback,
this.listOfActiveNotifications = const [],
this.listOfPlannedNotifications = const []}) { this.listOfPlannedNotifications = const []}) {
_startTimer(); _startTimer();
} }
@ -30,7 +33,8 @@ class FirebaseNotificationService
} }
@override @override
Future<void> pushNotification(NotificationModel notification) async { Future<void> pushNotification(NotificationModel notification,
[Function(NotificationModel model)? onNewNotification]) async {
try { try {
CollectionReference notifications = FirebaseFirestore.instance CollectionReference notifications = FirebaseFirestore.instance
.collection(FirebaseCollectionNames.activeNotifications); .collection(FirebaseCollectionNames.activeNotifications);
@ -41,6 +45,8 @@ class FirebaseNotificationService
await notifications.doc(notification.id).set(notificationMap); await notifications.doc(notification.id).set(notificationMap);
listOfActiveNotifications.add(notification); listOfActiveNotifications.add(notification);
onNewNotification?.call(notification) ??
newNotificationCallback(notification);
notifyListeners(); notifyListeners();
} catch (e) { } catch (e) {
debugPrint('Error creating document: $e'); debugPrint('Error creating document: $e');
@ -191,7 +197,8 @@ class FirebaseNotificationService
for (NotificationModel notification in plannedNotifications) { for (NotificationModel notification in plannedNotifications) {
if (notification.scheduledFor!.isBefore(currentTime) || if (notification.scheduledFor!.isBefore(currentTime) ||
notification.scheduledFor!.isAtSameMomentAs(currentTime)) { notification.scheduledFor!.isAtSameMomentAs(currentTime)) {
await pushNotification(notification); await pushNotification(notification, newNotificationCallback);
await deletePlannedNotification(notification); await deletePlannedNotification(notification);
//Plan new recurring notification instance //Plan new recurring notification instance

View file

@ -21,7 +21,6 @@ dependencies:
firebase_storage: ^11.0.14 firebase_storage: ^11.0.14
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
provider: ^6.1.2
flutter_notification_center: flutter_notification_center:
path: ../flutter_notification_center path: ../flutter_notification_center