From 58ddd9391d0329053e20343cfcaae41de9dd86f5 Mon Sep 17 00:00:00 2001 From: "J. A. Messias" Date: Mon, 23 Sep 2024 16:07:52 -0300 Subject: [PATCH] fix homePage nav on enroll_cond --- lib/backend/api_requests/api_calls.dart | 3 ++ .../firebase_messaging_service.dart | 3 ++ .../notifications/notification_service.dart | 2 ++ lib/main.dart | 14 +++++++- .../no_connection_page.dart | 32 ------------------- .../reception_page/reception_page_widget.dart | 8 +++++ 6 files changed, 29 insertions(+), 33 deletions(-) delete mode 100644 lib/pages/no_connection_page/no_connection_page.dart diff --git a/lib/backend/api_requests/api_calls.dart b/lib/backend/api_requests/api_calls.dart index 2f5d1bf9..0a001a68 100644 --- a/lib/backend/api_requests/api_calls.dart +++ b/lib/backend/api_requests/api_calls.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'dart:developer'; import 'package:flutter/foundation.dart'; +import 'package:hub/backend/notifications/firebase_messaging_service.dart'; import 'package:hub/shared/helpers/db_helper.dart'; import 'package:hub/shared/utils/validator_util.dart'; @@ -674,6 +675,7 @@ class LoginCall { final password = AppState().passwd; final type = AppState().deviceType; final description = AppState().deviceDescription; + final token = FirebaseMessagingService.getToken(); return ApiManager.instance.makeApiCall( callName: 'login', @@ -686,6 +688,7 @@ class LoginCall { 'email': email, 'password': password, 'uuid': devUUID, + 'token': token, 'tipo': type, 'descricao': description, }, diff --git a/lib/backend/notifications/firebase_messaging_service.dart b/lib/backend/notifications/firebase_messaging_service.dart index 36464962..3dd3ad68 100644 --- a/lib/backend/notifications/firebase_messaging_service.dart +++ b/lib/backend/notifications/firebase_messaging_service.dart @@ -56,6 +56,9 @@ class FirebaseMessagingService { // }); } + static Future getToken() async => + await FirebaseMessaging.instance.getToken() ?? ''; + Future updateDeviceToken() async { try { final String? deviceToken = await _firebaseMessaging.getToken(); diff --git a/lib/backend/notifications/notification_service.dart b/lib/backend/notifications/notification_service.dart index 32398ec5..c02ff32d 100644 --- a/lib/backend/notifications/notification_service.dart +++ b/lib/backend/notifications/notification_service.dart @@ -255,6 +255,8 @@ Future onMessageReceived( break; case 'enroll_cond': + AppState().haveLocal = true; + AppState().context!.go('/homePage'); break; default: break; diff --git a/lib/main.dart b/lib/main.dart index 54bb6db8..efb1e9ac 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -55,6 +55,12 @@ Future initializeApp() async { } Future foregroundHandleMessage(RemoteMessage message) async { + log('Foreground message received: ${message.data['click_action']}'); + + if (message.data['click_action'] == 'enroll_cond') { + AppState().haveLocal = true; + AppState().context!.go('/homePage'); + } if (!Platform.isIOS) { NotificationService.show( title: message.notification!.title!, @@ -63,7 +69,13 @@ Future foregroundHandleMessage(RemoteMessage message) async { } } -Future _backgroundHandleMessage(RemoteMessage message) async {} +Future _backgroundHandleMessage(RemoteMessage message) async { + log('Background message received: ${message.data}'); + if (message.data['click_action'] == 'enroll_cond') { + AppState().haveLocal = true; + AppState().context!.go('/homePage'); + } +} void main() async { await initializeApp(); diff --git a/lib/pages/no_connection_page/no_connection_page.dart b/lib/pages/no_connection_page/no_connection_page.dart deleted file mode 100644 index 5428e2d7..00000000 --- a/lib/pages/no_connection_page/no_connection_page.dart +++ /dev/null @@ -1,32 +0,0 @@ -import 'package:flutter/material.dart'; - -class NoConnectionScreen extends StatelessWidget { - const NoConnectionScreen({Key? key}) : super(key: key); - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: const Text('Sem Conexão'), - ), - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Icon(Icons.signal_wifi_off, size: 80), - const SizedBox(height: 20), - const Text( - 'Você está offline. Verifique sua conexão com a internet.'), - const SizedBox(height: 20), - ElevatedButton( - onPressed: () { - // Tente reconectar - }, - child: const Text('Tentar Novamente'), - ), - ], - ), - ), - ); - } -} diff --git a/lib/pages/reception_page/reception_page_widget.dart b/lib/pages/reception_page/reception_page_widget.dart index aa0cc481..19eef6ed 100644 --- a/lib/pages/reception_page/reception_page_widget.dart +++ b/lib/pages/reception_page/reception_page_widget.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:hub/backend/api_requests/api_calls.dart'; +import 'package:hub/backend/notifications/firebase_messaging_service.dart'; import 'package:hub/components/atomic_components/shared_components_atoms/atom_image_svg_theme.dart'; import 'package:hub/flutter_flow/flutter_flow_theme.dart'; import 'package:hub/flutter_flow/flutter_flow_util.dart'; @@ -17,8 +18,15 @@ class ReceptionPageWidget extends StatefulWidget { } class _ReceptionPageWidgetState extends State { + @override + void initState() { + super.initState(); + FirebaseMessagingService().updateDeviceToken(); + } + @override Widget build(BuildContext context) { + AppState().context = context; return ChangeNotifierProvider( create: (context) => ReceptionPageModel(), child: Scaffold(