firebase_fcm_client 0.0.2
firebase_fcm_client: ^0.0.2 copied to clipboard
Firebase Cloud Messaging (FCM) HTTP v1 API client for Dart with secure service account impersonation. Send notifications to devices, topics, and conditions without managing private keys.
example/main.dart
import 'package:firebase_fcm_client/firebase_fcm_client.dart';
Future<void> main() async {
print('🔔 FCM Notification Example\n');
try {
// Initialize with impersonation
final auth = await ServiceAccountAuth.fromImpersonation(
serviceAccountEmail: 'fcm-sa@YOUR_PROJECT.iam.gserviceaccount.com',
projectId: 'YOUR_PROJECT_ID',
);
print('✅ Authenticated\n');
final fcm = FCMClient(auth: auth, projectId: 'YOUR_PROJECT_ID');
// Example 1: Send to device
print('📤 Sending to device...');
final result = await fcm.sendToToken(
'YOUR_DEVICE_TOKEN',
title: '🎉 Hello',
body: 'This is a test notification',
data: {
'action': 'open_app',
'timestamp': DateTime.now().toIso8601String(),
},
);
print('✅ Sent: $result\n');
// Example 2: Send to topic
print('📤 Sending to topic...');
final topicResult = await fcm.sendToTopic(
'news',
title: '📢 News Update',
body: 'Important announcement',
);
print('✅ Sent: $topicResult\n');
await auth.close();
print('✨ Examples completed!');
} catch (e) {
print('❌ Error: $e');
}
}