diff --git a/lib/main.dart b/lib/main.dart index d638147..878e93e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -32,7 +32,31 @@ void main() async { AndroidInitializationSettings('@mipmap/ic_launcher'); const InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid); - await flutterLocalNotificationsPlugin.initialize(initializationSettings); + await flutterLocalNotificationsPlugin.initialize(initializationSettings, + onDidReceiveNotificationResponse: (NotificationResponse response) { + // Handle the notification tap + if (response.payload != null) { + Navigator.push( + navigatorKey.currentState!.context, + MaterialPageRoute( + builder: (BuildContext context) => UserProfilePage( + userId: response.payload, + titleText: 'Your new Match', + ), + ), + ); + } + }); + + // create notification channel + const channel = AndroidNotificationChannel( + 'new_match_info_channel_id', + 'New Match Info Channel', + ); + flutterLocalNotificationsPlugin + .resolvePlatformSpecificImplementation< + AndroidFlutterLocalNotificationsPlugin>() + ?.createNotificationChannel(channel); // Initialize the singleton instance with a dummy userId. // The actual userId is set later in the HomePage. diff --git a/lib/pages/user_profile_page.dart b/lib/pages/user_profile_page.dart index 2abb633..5f026aa 100644 --- a/lib/pages/user_profile_page.dart +++ b/lib/pages/user_profile_page.dart @@ -20,9 +20,10 @@ import 'user_data_page.dart'; import 'user_vision_page.dart'; class UserProfilePage extends StatefulWidget { - const UserProfilePage({super.key, this.userId}); + const UserProfilePage({super.key, this.userId, this.titleText}); final String? userId; + final String? titleText; @override State createState() => _UserProfilePageState(); @@ -34,6 +35,7 @@ class _UserProfilePageState extends State { bool isLoading = true; late bool isOwner; late String _userId; + late String _titleText; @override void initState() { @@ -45,6 +47,9 @@ class _UserProfilePageState extends State { // Load user data on initialization _loadUserData(); + + _titleText = (widget.titleText ?? + (isOwner ? 'My Profile Information' : 'Profile Information')); } Future _loadUserData() async { @@ -230,7 +235,7 @@ class _UserProfilePageState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text(isOwner ? 'My Profile Information' : 'Profile Information'), + title: Text(_titleText), ), body: isLoading ? const Center(child: CircularProgressIndicator()) diff --git a/lib/services/swipe_stream_service.dart b/lib/services/swipe_stream_service.dart index fedaa7d..4298f4e 100644 --- a/lib/services/swipe_stream_service.dart +++ b/lib/services/swipe_stream_service.dart @@ -3,8 +3,6 @@ import 'package:flutter/foundation.dart' show debugPrint, kDebugMode; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import '../constants.dart'; -import '../main.dart'; -import '../utils/helper_dialogs.dart'; import 'user_service.dart'; class SwipeStreamService { @@ -49,60 +47,37 @@ class SwipeStreamService { void _showNotification(String swipeId) async { String matchName = await UserService.getUserName(swipeId); const AndroidNotificationDetails androidPlatformChannelSpecifics = - AndroidNotificationDetails( - 'new_matches_channel_id', 'new_match_info', + AndroidNotificationDetails('new_matches_channel_id', 'new_match_info', channelShowBadge: true, visibility: NotificationVisibility.private, - importance: Importance.max, priority: Priority.high, showWhen: true); const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics); await flutterLocalNotificationsPlugin.show(0, 'New Match', - 'You have a new match with $matchName', platformChannelSpecifics); + 'You have a new match with $matchName', platformChannelSpecifics, + payload: swipeId); } void listenToSwipes() { _subscription = swipesStream.listen( - (swipes) async { - List userNames = []; - String tempUser = ''; - + (swipes) { if (!matchesRead) { for (var swipe in swipes) { - debugPrint('Init Match Notify --> ${swipe.id}'); + if (kDebugMode) debugPrint('Init Match Notify --> ${swipe.id}'); _matchesInDB.add(swipe.id); - tempUser = await UserService.getUserName(swipe.id); - userNames.add('INIT $tempUser'); } - if (kDebugMode) { - showErrorSnackBar( - navigatorKey.currentContext!, - userNames.join(', '), - ); - } - matchesRead = true; } else { for (var swipe in swipes) { - tempUser = await UserService.getUserName(swipe.id); - if (!_matchesInDB.contains(swipe.id)) { - userNames.add('NEW! $tempUser'); - debugPrint('NEW Match Notify --> ${swipe.id}'); + if (kDebugMode) debugPrint('NEW Match Notify --> ${swipe.id}'); _matchesInDB.add(swipe.id); _showNotification(swipe.id); } else { - userNames.add('OLD $tempUser'); - debugPrint('Old Match Notify --> ${swipe.id}'); + if (kDebugMode) debugPrint('Old Match Notify --> ${swipe.id}'); } } - if (kDebugMode) { - showErrorSnackBar( - navigatorKey.currentContext!, - userNames.join(', '), - ); - } } }, ); @@ -115,7 +90,7 @@ class SwipeStreamService { void addUser(String userId) { if (!_matchesInDB.contains(userId)) { - debugPrint('Notify: SKIP Match Notify for --> $userId'); + if (kDebugMode) debugPrint('Notify: SKIP Match Notify for --> $userId'); _matchesInDB.add(userId); } }