import 'dart:io'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:provider/provider.dart'; import 'package:pulse/SplashScreen.dart'; import 'package:pulse/Notifier/auth_provider.dart'; import 'package:pulse/Notifier/theme_provider.dart'; import 'package:pulse/Notifier/ProfileProvider.dart'; import 'package:pulse/Notifier/webProvider.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:pulse/firebase_options.dart'; import 'package:pulse/utils/SharedpreferencesService.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'Screens/home_screen.dart'; // Needed to navigate outside BuildContext final GlobalKey navigatorKey = GlobalKey(); // Initialize the notification plugin final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); // Handle background notifications Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { if (Firebase.apps.isEmpty) { await Firebase.initializeApp(); } print("📬 Background message: ${message.messageId}"); } Future main() async { WidgetsFlutterBinding.ensureInitialized(); // if (Firebase.apps.isEmpty) { // await Firebase.initializeApp( // options: const FirebaseOptions( // apiKey: "2f663c764bb77355c82d30b2068b15427363ccfd", // appId: "1:207322820833:android:82516ed462f6ae6e4447a0", // messagingSenderId: "207322820833", // projectId: "pulse-5cf9a", // ), // ); // debugPrint(" Firebase initialized."); // } if (Firebase.apps.isEmpty) { await Firebase.initializeApp( name: "pulse-5cf9a", options: DefaultFirebaseOptions.currentPlatform, ); } // Setup local notifications const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher'); const InitializationSettings initializationSettings = InitializationSettings( android: initializationSettingsAndroid, iOS: DarwinInitializationSettings(), ); await flutterLocalNotificationsPlugin.initialize(initializationSettings); // Local notifications setup const AndroidInitializationSettings initSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher'); const InitializationSettings initSettings = InitializationSettings( android: initSettingsAndroid, iOS: DarwinInitializationSettings(), ); await flutterLocalNotificationsPlugin.initialize( initSettings, onDidReceiveNotificationResponse: (NotificationResponse response) { if (response.payload != null) { final data = jsonDecode(response.payload!); NotificationHandler.handle(data); } }, ); // Setup FCM FirebaseMessaging messaging = FirebaseMessaging.instance; await messaging.requestPermission(alert: true, badge: true, sound: true); if (Platform.isAndroid) { FirebaseMessaging.instance.getToken().then((value) { String? token = value; // print("Androidfbstoken:{$token}"); SharedPreferencesService.instance.saveString("fbstoken", token!); // toast(BuildContext , token); }); } else { FirebaseMessaging.instance.getAPNSToken().then((value) { String? token = value; // print("IOSfbstoken:{$token}"); SharedPreferencesService.instance.saveString("fbstoken", token ?? ""); // toast(BuildContext , token); }); } // print("🔥 FCM Token: $token"); // Background handler FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); // Foreground notification FirebaseMessaging.onMessage.listen((RemoteMessage message) async { print("💬 Foreground message: ${message.notification?.title}"); RemoteNotification? notification = message.notification; AndroidNotification? android = message.notification?.android; if (notification != null && android != null) { flutterLocalNotificationsPlugin.show( notification.hashCode, notification.title, notification.body, const NotificationDetails( android: AndroidNotificationDetails( 'pulse_channel', // channel id 'Pulse Notifications', // channel name importance: Importance.max, priority: Priority.high, ), ), payload: jsonEncode(message.data), ); } }); // notification while app is in background FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { print(" Notification clicked: ${message.data}"); }); // InAppWebViewController.setWebContentsDebuggingEnabled(true); runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MultiProvider( providers: [ ChangeNotifierProvider(create: (_) => AuthProvider()), ChangeNotifierProvider(create: (_) => ThemeProvider()), ChangeNotifierProvider( create: (_) => ProfileProvider(), ), ChangeNotifierProvider(create: (_) => WebErpProvider()), ], child: Consumer( builder: (context, themeProvider, child) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Web Grid Pulse', theme: themeProvider.isDark ? ThemeData.dark() : ThemeData.light().copyWith( scaffoldBackgroundColor: const Color(0xFFF5F8FC), ), home: const SplashScreen(), ); }, ), ); } } class NotificationHandler { static void handle(Map data) { final type = data['type']; final value = data['type_value']; final notifId = data['notification_id']; debugPrint("🔗 Handling Notification:"); debugPrint("type=$type, value=$value, id=$notifId"); // Decide what to do based on the type if (type == 'web' && value != null) { navigatorKey.currentState?.push( MaterialPageRoute( builder: (_) => HomeScreen( sessionId: '', staffId: '', notificationUrl: value, ), ), ); } } }