diff --git a/README.md b/README.md index 6eff803..8903d30 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ The `NotificationConfig` has its own parameters, as specified below: | seperateNotificationsWithDivider | If true notifications will be seperated with dividers within the notification center | | translations | The translations that will be used | | notificationWidgetBuilder | The widget that defines the styles and logic for every notification | +| enableNotificationPopups | If set to false no popups will be shown if a new notification is pushed | | showAsSnackBar | If true notifications popups will show as snackbar. If false shown as dialog| The `notificationWidgetBuilder` expects the following parameters, as specified below: diff --git a/packages/flutter_notification_center/example/lib/main.dart b/packages/flutter_notification_center/example/lib/main.dart index d4c331f..cbfa575 100644 --- a/packages/flutter_notification_center/example/lib/main.dart +++ b/packages/flutter_notification_center/example/lib/main.dart @@ -52,34 +52,18 @@ class NotificationCenterDemo extends StatefulWidget { class _NotificationCenterDemoState extends State { late NotificationConfig config; + late PopupHandler popupHandler; @override void initState() { super.initState(); - var service = FirebaseNotificationService( - newNotificationCallback: (notification) { - if (config.showAsSnackBar) { - ScaffoldMessenger.of(context).showSnackBar( - NotificationSnackbar( - title: notification.title, - body: notification.body, - datetimePublished: DateTime.now(), - ), - ); - } else { - showDialog( - context: context, - builder: (context) => NotificationDialog( - title: notification.title, - body: notification.body, - datetimePublished: notification.dateTimePushed, - ), - ); - } - }, - ); + var service = + FirebaseNotificationService(newNotificationCallback: (notification) { + popupHandler.handleNotificationPopup(notification); + }); config = NotificationConfig( service: service, + enableNotificationPopups: true, showAsSnackBar: false, notificationWidgetBuilder: (notification, context) => CustomNotificationWidget( @@ -105,6 +89,7 @@ class _NotificationCenterDemoState extends State { ), seperateNotificationsWithDivider: true, ); + popupHandler = PopupHandler(context: context, config: config); } @override diff --git a/packages/flutter_notification_center/lib/flutter_notification_center.dart b/packages/flutter_notification_center/lib/flutter_notification_center.dart index 5ab77fc..8e84e2f 100644 --- a/packages/flutter_notification_center/lib/flutter_notification_center.dart +++ b/packages/flutter_notification_center/lib/flutter_notification_center.dart @@ -8,6 +8,7 @@ export "src/models/notification_theme.dart"; export "src/models/notification_translation.dart"; export "src/notification_bell.dart"; export "src/notification_dialog.dart"; +export "src/popup_handler.dart"; export "src/notification_snackbar.dart"; export "src/notification_detail.dart"; export "src/notification_bell_story.dart"; diff --git a/packages/flutter_notification_center/lib/src/models/notification_config.dart b/packages/flutter_notification_center/lib/src/models/notification_config.dart index b40fe30..3bfa40a 100644 --- a/packages/flutter_notification_center/lib/src/models/notification_config.dart +++ b/packages/flutter_notification_center/lib/src/models/notification_config.dart @@ -10,13 +10,13 @@ class NotificationConfig { /// to use. The [style] parameter is optional and defines the style of the /// notification. The [translations] parameter is also optional and provides /// translations for notification messages. - const NotificationConfig({ - required this.service, - this.seperateNotificationsWithDivider = true, - this.translations = const NotificationTranslations(), - this.notificationWidgetBuilder, - this.showAsSnackBar = true, - }); + const NotificationConfig( + {required this.service, + this.seperateNotificationsWithDivider = true, + this.translations = const NotificationTranslations(), + this.notificationWidgetBuilder, + this.showAsSnackBar = true, + this.enableNotificationPopups = true}); /// The notification service to use for delivering notifications. final NotificationService service; @@ -33,4 +33,7 @@ class NotificationConfig { /// Whether to show notifications as snackbars. If false show notifications as a dialog. final bool showAsSnackBar; + + /// Whether to show notification popups. + final bool enableNotificationPopups; } diff --git a/packages/flutter_notification_center/lib/src/popup_handler.dart b/packages/flutter_notification_center/lib/src/popup_handler.dart new file mode 100644 index 0000000..863178f --- /dev/null +++ b/packages/flutter_notification_center/lib/src/popup_handler.dart @@ -0,0 +1,37 @@ +// Define a PopupHandler class to handle notification popups +import 'package:flutter/material.dart'; + +import 'package:flutter_notification_center/flutter_notification_center.dart'; + +class PopupHandler { + final BuildContext context; + final NotificationConfig config; + + PopupHandler({ + required this.context, + required this.config, + }); + + void handleNotificationPopup(NotificationModel notification) { + if (!config.enableNotificationPopups) return; + + if (config.showAsSnackBar) { + ScaffoldMessenger.of(context).showSnackBar( + NotificationSnackbar( + title: notification.title, + body: notification.body, + datetimePublished: DateTime.now(), + ), + ); + } else { + showDialog( + context: context, + builder: (context) => NotificationDialog( + title: notification.title, + body: notification.body, + datetimePublished: notification.dateTimePushed, + ), + ); + } + } +} 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 15ec201..b2cc09a 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 @@ -46,7 +46,7 @@ class FirebaseNotificationService listOfActiveNotifications.add(notification); - //Show popup with notification contents + //Show popup with notification conte if (onNewNotification != null) { onNewNotification(notification); } else {