import 'dart:convert'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:healthcare_pharmacy/settings.dart'; import 'package:shared_preferences/shared_preferences.dart'; class PushNotificationService { final FirebaseMessaging _fcm = FirebaseMessaging.instance; final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); // Constructor to initialize the FCM token PushNotificationService() { _initFCMToken(); } Future _initFCMToken() async { // Get the stored FCM token AppSettings.fcmId = await _getStoredFCMToken(); // If FCM token is not available, request a new one if (AppSettings.fcmId.isEmpty) { await _fcm.getToken().then((token) { print('FCM Token: $token'); // Store the FCM token directly in AppSettings AppSettings.fcmId = token!; _storeFCMToken(token); }); } } Future _storeFCMToken(String token) async { // Store the FCM token using SharedPreferences SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setString('fcmToken', token); } Future _getStoredFCMToken() async { // Retrieve the stored FCM token from SharedPreferences SharedPreferences prefs = await SharedPreferences.getInstance(); return prefs.getString('fcmToken') ?? ''; } Future initialize() async { // Request permission for user notifications await _fcm.requestPermission(); // Get the FCM token and handle incoming messages while the app is in the foreground _fcm.getToken().then((token) { print('FCM Token: $token'); // Do something with the token (e.g., send it to your server) }); // Listen to incoming messages while the app is in the foreground FirebaseMessaging.onMessage.listen((RemoteMessage message) { print("data::::$message"); // var payload = message.data.toString(); // // Handle the incoming message // Map notificationPayload = jsonDecode(payload); // Access the extra data using the keys var payload=message.data; var extramessage; if(payload.isNotEmpty){ extramessage = message.data['extraKey1']; String? extraValue2 = message.data['extraKey2']; } // Show a local notification with the received message _showNotification(message.notification?.title, message.notification?.body,extramessage); }); // Handle messages when the app is in the background or terminated FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { var payload = message.data.toString(); if (payload != null) { // Parse the JSON payload Map notificationPayload = jsonDecode(payload); // Access the extra data using the keys String? extraValue1 = notificationPayload['extraKey1']; String? extraValue2 = notificationPayload['extraKey2']; // Do something with the extra data, e.g., display it in a dialog or use it in your app logic. } // Show a local notification with the received message // _showNotification(message.notification?.title, message.notification?.body); }); } // Method to show a local notification using flutter_local_notifications Future _showNotification(String? title, String? body,String extramessage) async { const AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails( 'healthpharma', // Replace with your channel ID 'healthpharmanotifications', // Replace with your channel name importance: Importance.high, priority: Priority.high, showWhen: false, icon: "logo" ); const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics); Map notificationPayload = { "body": body, "title": title, "extraKey1": extramessage, "extraKey2": "Extra Value 2", // Add more key-value pairs as needed }; await _flutterLocalNotificationsPlugin.show( 0, // notification ID (can be any unique ID) title, // notification title body, // notification body platformChannelSpecifics, payload: jsonEncode(notificationPayload), // optional, you can pass data or identifier related to the notification ); } }