From d4a6e236a0598ff58be14c1c079306642754d79d Mon Sep 17 00:00:00 2001 From: anhtunz Date: Tue, 8 Apr 2025 10:21:48 +0700 Subject: [PATCH] feat(notification): handle notification onclick event --- .../devices/devices_manager_screen.dart | 3 +- lib/feature/main/main_screen.dart | 26 +++++++++++++---- lib/main.dart | 29 ++++++++++--------- .../services/notification_services.dart | 14 +++++---- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/lib/feature/devices/devices_manager_screen.dart b/lib/feature/devices/devices_manager_screen.dart index b2933ba..b5021c2 100644 --- a/lib/feature/devices/devices_manager_screen.dart +++ b/lib/feature/devices/devices_manager_screen.dart @@ -225,8 +225,7 @@ class _DevicesManagerScreenState extends State { void getUserRole() async { role = await apiServices.getUserRole(); - devicesManagerBloc.sinkUserRole.add(role);F: - cd + devicesManagerBloc.sinkUserRole.add(role); } double getTableHeight(int dataLength){ diff --git a/lib/feature/main/main_screen.dart b/lib/feature/main/main_screen.dart index c979760..2177eea 100644 --- a/lib/feature/main/main_screen.dart +++ b/lib/feature/main/main_screen.dart @@ -2,12 +2,15 @@ import 'dart:developer'; +import 'package:firebase_core/firebase_core.dart'; +import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:badges/badges.dart' as badges; import 'package:persistent_bottom_nav_bar/persistent_bottom_nav_bar.dart'; import '../../product/permission/notification_permission.dart'; +import '../../product/services/notification_services.dart'; import '../settings/profile/profile_model.dart'; import '../../product/extension/context_extension.dart'; import '../../bloc/home_bloc.dart'; @@ -41,8 +44,22 @@ class MainScreen extends StatefulWidget { State createState() => _MainScreenState(); } +PersistentTabController controller = PersistentTabController(initialIndex: 0); + +@pragma('vm:entry-point') +Future firebaseMessagingBackgroundHandler(RemoteMessage message) async { + log("Full background message payload: ${message.toMap()}"); + await Firebase.initializeApp(); + final notificationServices = NotificationServices(); + await notificationServices.initLocalNotifications(controller); + await notificationServices.showNotification(message); + log("Background message handled: ${message.data['title']}"); +} + + class _MainScreenState extends State with WidgetsBindingObserver { APIServices apiServices = APIServices(); + final NotificationServices notificationServices = NotificationServices(); late MainBloc mainBloc; bool isVN = true; bool isLight = true; @@ -81,6 +98,9 @@ class _MainScreenState extends State with WidgetsBindingObserver { initialCheck(); getBellNotification(); mainBloc.getUserProfile(); + notificationServices.initLocalNotifications(controller); + notificationServices.firebaseInit(context); + NotificationServices().setupInteractMessage(controller); } @override @@ -104,7 +124,7 @@ class _MainScreenState extends State with WidgetsBindingObserver { WidgetsBinding.instance.removeObserver(this); } - PersistentTabController controller = PersistentTabController(initialIndex: 0); + List _navBarsItems() { return [ PersistentBottomNavBarItem( @@ -180,10 +200,6 @@ class _MainScreenState extends State with WidgetsBindingObserver { child: const InterFamilyScreen(), blocBuilder: () => InterFamilyBloc(), ), - /*BlocProvider( - child: const NotificationScreen(), - blocBuilder: () => NotificationBloc(), - ),*/ ]; } diff --git a/lib/main.dart b/lib/main.dart index ea86159..b95fc83 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -4,7 +4,8 @@ import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; -import 'package:sfm_app/product/services/api_services.dart'; +import 'feature/main/main_screen.dart'; +import 'product/services/api_services.dart'; import 'product/services/notification_services.dart'; import 'product/services/theme_services.dart'; import 'product/services/language_services.dart'; @@ -16,8 +17,8 @@ void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); FirebaseMessaging - .onBackgroundMessage(_firebaseMessagingBackgroundHandler); - NotificationServices().setupInteractMessage(); + .onBackgroundMessage(firebaseMessagingBackgroundHandler); + // NotificationServices().setupInteractMessage(); runApp( BlocProvider( @@ -27,15 +28,15 @@ void main() async { ); } -@pragma('vm:entry-point') -Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { - log("Full background message payload: ${message.toMap()}"); - await Firebase.initializeApp(); - final notificationServices = NotificationServices(); - await notificationServices.initLocalNotifications(); - await notificationServices.showNotification(message); - log("Background message handled: ${message.data['title']}"); -} +// @pragma('vm:entry-point') +// Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { +// log("Full background message payload: ${message.toMap()}"); +// await Firebase.initializeApp(); +// final notificationServices = NotificationServices(); +// await notificationServices.initLocalNotifications(); +// await notificationServices.showNotification(message); +// log("Background message handled: ${message.data['title']}"); +// } class MyApp extends StatefulWidget { const MyApp({super.key}); @@ -73,8 +74,8 @@ class _MyAppState extends State { void initState() { super.initState(); mainBloc = BlocProvider.of(context); - notificationServices.initLocalNotifications(); - notificationServices.firebaseInit(context); + // notificationServices.initLocalNotifications(); + // notificationServices.firebaseInit(context); // notificationServices.setupInteractMessage(); notificationServices.getDeviceToken().then((token){ print("Firebase Token: $token"); diff --git a/lib/product/services/notification_services.dart b/lib/product/services/notification_services.dart index a02915c..e9557f9 100644 --- a/lib/product/services/notification_services.dart +++ b/lib/product/services/notification_services.dart @@ -3,12 +3,13 @@ import 'dart:math' as math; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; +import 'package:persistent_bottom_nav_bar/persistent_bottom_nav_bar.dart'; class NotificationServices { FirebaseMessaging messaging = FirebaseMessaging.instance; final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); - Future initLocalNotifications() async { + Future initLocalNotifications(PersistentTabController controller) async { const AndroidInitializationSettings androidInitializationSettings = AndroidInitializationSettings('@mipmap/ic_launcher'); const DarwinInitializationSettings iosInitializationSettings = DarwinInitializationSettings(); const InitializationSettings initializationSettings = InitializationSettings( @@ -20,7 +21,7 @@ class NotificationServices { initializationSettings, onDidReceiveNotificationResponse: (NotificationResponse response) { dev.log("Người dùng click thông báo ở foreground với payload: ${response.payload}"); - handleMessage(response.payload); + handleMessage(response.payload,controller); }, ); dev.log("Local notifications initialized"); @@ -126,18 +127,19 @@ class NotificationServices { } } - void handleMessage(String? payload) { + void handleMessage(String? payload, PersistentTabController controller) { dev.log("Handling notification tap with payload: $payload"); + controller.jumpToTab(1); } - Future setupInteractMessage() async { + Future setupInteractMessage(PersistentTabController controller) async { // Khi app terminated RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage(); if (initialMessage != null) { WidgetsBinding.instance.addPostFrameCallback((_) { try { - handleMessage(initialMessage.data['type']); + handleMessage(initialMessage.data['type'],controller); } catch (e, stack) { dev.log("Error handling initial message: $e\n$stack"); } @@ -146,7 +148,7 @@ class NotificationServices { // Khi app ở background FirebaseMessaging.onMessageOpenedApp.listen((message) { try { - handleMessage(message.data['type']); + handleMessage(message.data['type'],controller); } catch (e, stack) { dev.log("Error in onMessageOpenedApp: $e\n$stack"); }