Tap on Notification opens UserProfile

master
Rafael 2024-07-12 22:32:07 +02:00
parent f866e187ae
commit a3705aab95
3 changed files with 40 additions and 36 deletions

View File

@ -32,7 +32,31 @@ void main() async {
AndroidInitializationSettings('@mipmap/ic_launcher'); AndroidInitializationSettings('@mipmap/ic_launcher');
const InitializationSettings initializationSettings = const InitializationSettings initializationSettings =
InitializationSettings(android: initializationSettingsAndroid); 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. // Initialize the singleton instance with a dummy userId.
// The actual userId is set later in the HomePage. // The actual userId is set later in the HomePage.

View File

@ -20,9 +20,10 @@ import 'user_data_page.dart';
import 'user_vision_page.dart'; import 'user_vision_page.dart';
class UserProfilePage extends StatefulWidget { class UserProfilePage extends StatefulWidget {
const UserProfilePage({super.key, this.userId}); const UserProfilePage({super.key, this.userId, this.titleText});
final String? userId; final String? userId;
final String? titleText;
@override @override
State<UserProfilePage> createState() => _UserProfilePageState(); State<UserProfilePage> createState() => _UserProfilePageState();
@ -34,6 +35,7 @@ class _UserProfilePageState extends State<UserProfilePage> {
bool isLoading = true; bool isLoading = true;
late bool isOwner; late bool isOwner;
late String _userId; late String _userId;
late String _titleText;
@override @override
void initState() { void initState() {
@ -45,6 +47,9 @@ class _UserProfilePageState extends State<UserProfilePage> {
// Load user data on initialization // Load user data on initialization
_loadUserData(); _loadUserData();
_titleText = (widget.titleText ??
(isOwner ? 'My Profile Information' : 'Profile Information'));
} }
Future<void> _loadUserData() async { Future<void> _loadUserData() async {
@ -230,7 +235,7 @@ class _UserProfilePageState extends State<UserProfilePage> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text(isOwner ? 'My Profile Information' : 'Profile Information'), title: Text(_titleText),
), ),
body: isLoading body: isLoading
? const Center(child: CircularProgressIndicator()) ? const Center(child: CircularProgressIndicator())

View File

@ -3,8 +3,6 @@ import 'package:flutter/foundation.dart' show debugPrint, kDebugMode;
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import '../constants.dart'; import '../constants.dart';
import '../main.dart';
import '../utils/helper_dialogs.dart';
import 'user_service.dart'; import 'user_service.dart';
class SwipeStreamService { class SwipeStreamService {
@ -49,60 +47,37 @@ class SwipeStreamService {
void _showNotification(String swipeId) async { void _showNotification(String swipeId) async {
String matchName = await UserService.getUserName(swipeId); String matchName = await UserService.getUserName(swipeId);
const AndroidNotificationDetails androidPlatformChannelSpecifics = const AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails( AndroidNotificationDetails('new_matches_channel_id', 'new_match_info',
'new_matches_channel_id', 'new_match_info',
channelShowBadge: true, channelShowBadge: true,
visibility: NotificationVisibility.private, visibility: NotificationVisibility.private,
importance: Importance.max,
priority: Priority.high, priority: Priority.high,
showWhen: true); showWhen: true);
const NotificationDetails platformChannelSpecifics = const NotificationDetails platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics); NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(0, 'New Match', 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() { void listenToSwipes() {
_subscription = swipesStream.listen( _subscription = swipesStream.listen(
(swipes) async { (swipes) {
List<String> userNames = [];
String tempUser = '';
if (!matchesRead) { if (!matchesRead) {
for (var swipe in swipes) { for (var swipe in swipes) {
debugPrint('Init Match Notify --> ${swipe.id}'); if (kDebugMode) debugPrint('Init Match Notify --> ${swipe.id}');
_matchesInDB.add(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; matchesRead = true;
} else { } else {
for (var swipe in swipes) { for (var swipe in swipes) {
tempUser = await UserService.getUserName(swipe.id);
if (!_matchesInDB.contains(swipe.id)) { if (!_matchesInDB.contains(swipe.id)) {
userNames.add('NEW! $tempUser'); if (kDebugMode) debugPrint('NEW Match Notify --> ${swipe.id}');
debugPrint('NEW Match Notify --> ${swipe.id}');
_matchesInDB.add(swipe.id); _matchesInDB.add(swipe.id);
_showNotification(swipe.id); _showNotification(swipe.id);
} else { } else {
userNames.add('OLD $tempUser'); if (kDebugMode) debugPrint('Old Match Notify --> ${swipe.id}');
debugPrint('Old Match Notify --> ${swipe.id}');
} }
} }
if (kDebugMode) {
showErrorSnackBar(
navigatorKey.currentContext!,
userNames.join(', '),
);
}
} }
}, },
); );
@ -115,7 +90,7 @@ class SwipeStreamService {
void addUser(String userId) { void addUser(String userId) {
if (!_matchesInDB.contains(userId)) { if (!_matchesInDB.contains(userId)) {
debugPrint('Notify: SKIP Match Notify for --> $userId'); if (kDebugMode) debugPrint('Notify: SKIP Match Notify for --> $userId');
_matchesInDB.add(userId); _matchesInDB.add(userId);
} }
} }