You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
4.4 KiB
126 lines
4.4 KiB
|
|
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<void> _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<void> _storeFCMToken(String token) async {
|
|
// Store the FCM token using SharedPreferences
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString('fcmToken', token);
|
|
}
|
|
|
|
Future<String> _getStoredFCMToken() async {
|
|
// Retrieve the stored FCM token from SharedPreferences
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
return prefs.getString('fcmToken') ?? '';
|
|
}
|
|
Future<void> 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<String, dynamic> 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<String, dynamic> 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<void> _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<String, dynamic> 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
|
|
);
|
|
}
|
|
}
|