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: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<void> _configureApp() 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 {
@ -55,8 +53,16 @@ class NotificationCenterDemo extends StatefulWidget {
class _NotificationCenterDemoState extends State<NotificationCenterDemo> {
@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<FirebaseNotificationService>(context),
service: service,
notificationWidgetBuilder: (notification, context) =>
CustomNotificationWidget(
notification: notification,
@ -75,7 +81,7 @@ class _NotificationCenterDemoState extends State<NotificationCenterDemo> {
isReadDotColor: Colors.red,
showNotificationIcon: true,
),
notificationService: Provider.of<FirebaseNotificationService>(context),
notificationService: service,
notificationTranslations: const NotificationTranslations(),
context: context,
),
@ -93,6 +99,18 @@ class _NotificationCenterDemoState extends State<NotificationCenterDemo> {
],
),
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_core: ^2.5.0
firebase_storage: ^11.0.14
provider: ^6.1.2
flutter_notification_center:
path: ../

View file

@ -26,7 +26,8 @@ abstract class NotificationService with ChangeNotifier {
List<NotificationModel> 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<List<NotificationModel>> getActiveNotifications();

View file

@ -8,6 +8,8 @@ import '../config/firebase_collections.dart';
class FirebaseNotificationService
with ChangeNotifier
implements NotificationService {
final Function(NotificationModel) newNotificationCallback;
@override
List<NotificationModel> 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<void> pushNotification(NotificationModel notification) async {
Future<void> 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

View file

@ -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