right here is my code for the cloud perform:
// Fetch the primary identify of the consumer who created the appointment
const creatorSnapshot = await admin
.firestore()
.assortment(“customers”)
.doc(appointment.createdBy)
.get();
const creator = creatorSnapshot.information();
const creatorName = creator ? creator.first_name : “Somebody”;
const requestedUserId = appointment.createdBy;
// Get all customers to ship notification to, excluding the creator
const usersSnapshot = await admin.firestore().assortment(“customers”).get();
const tokens = [];
usersSnapshot.forEach((doc) => {
if (doc.id !== appointment.createdBy) {
const userToken = doc.information().fcmToken;
if (userToken) {
tokens.push(userToken);
}
}
});
if (tokens.size === 0) {
console.log(“No legitimate tokens discovered. Notification not despatched.”);
return null;
}
// Developing the FCM message
const message = {
notification: {
title: “New Session Out there”,
physique: ${creatorName} has created an appointment for ${appointment.exercises.be a part of( ", " )} at ${appointment.date_time .toDate() .toLocaleString()}, are you accessible?
,
},
information: {
kind: “new_appointment”,
appointmentId: appointmentId,
requestedUserId: requestedUserId,
},
tokens: tokens,
};
// Sending the FCM message
return admin
.messaging()
.sendMulticast(message)
.then((response) => {
console.log(response.successCount + ” messages had been despatched efficiently”);
});
And right here is my code for dealing with the notifications
// iOS Initialization
var initializationSettingsIOS = DarwinInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
onDidReceiveLocalNotification: onDidReceiveLocalNotification,
);
// request permissions
//for ios
if (Platform.isIOS) {
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
//for android
} else if (Platform.isAndroid) {
ultimate AndroidFlutterLocalNotificationsPlugin? androidImplementation =
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();
// ignore: unused_local_variable
ultimate bool? grantedNotificationPermission =
await androidImplementation?.requestNotificationsPermission();
}
// InitializationSettings for each platforms
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
saveUserTokenToFirestore();
print(‘step 1’);
// initialize the plugins
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse:
(NotificationResponse notificationResponse) {
if (notificationResponse.notificationResponseType ==
NotificationResponseType.selectedNotification) {
selectNotificationSubject.add(notificationResponse.payload);
}
},
);
onNotificationTap();
FirebaseMessaging.onMessage.hear((RemoteMessage message) {
print(‘step 2’);
showNotification(message);
});
FirebaseMessaging.onMessageOpenedApp.hear((RemoteMessage message) {
onNotificationTap();
});
// Android Notification Particulars
var androidDetails = const AndroidNotificationDetails(
‘new_appointment’,
‘Appointment Notifications’,
channelDescription: ‘Get notified about new appointments and updates’,
significance: Significance.max,
);
// iOS Notification Particulars
var iosDetails = const DarwinNotificationDetails(
presentAlert: true, presentSound: true, subtitle: ‘Notification’);
var notificationDetails =
NotificationDetails(android: androidDetails, iOS: iosDetails);
String payload = json.encode(message.information);
// Show the notification utilizing the flutterLocalNotificationsPlugin
await flutterLocalNotificationsPlugin.present(
message.hashCode,
message.notification?.title ?? ‘Default Title’,
message.notification?.physique ?? ‘Default physique’,
notificationDetails,
payload: payload,
);