smart_firebase_fcm 1.0.14
smart_firebase_fcm: ^1.0.14 copied to clipboard
A plug-and-play modular Firebase FCM package with local notifications, redirection, and manual feature toggles (analytics, crashlytics, etc.).
import 'package:flutter/material.dart';
import 'package:smart_firebase_fcm/smart_firebase_fcm.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Smart Firebase FCM Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _status = 'Not Initialized';
String? _token;
Future<void> _initializeFCM() async {
setState(() {
_status = 'Initializing...';
});
try {
// initialize() internally calls Firebase.initializeApp()
// Ensure you have added google-services.json (Android) and GoogleService-Info.plist (iOS)
await FCMInitializer.initialize(
onTap: (message) {
debugPrint('π Notification Tapped: ${message.data}');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Tapped: ${message.notification?.title ?? "No Title"}'),
),
);
},
enableIOSConfig: true,
showLocalNotificationsInForeground: true,
enableBadges: true,
// Optional: Custom Android icon
// androidNotificationIcon: '@mipmap/ic_launcher',
);
// Get the token
final token = await FCMInitializer.getDeviceToken();
setState(() {
_status = 'Initialized';
_token = token;
});
debugPrint('π± FCM Token: $_token');
} catch (e) {
setState(() {
_status = 'Error: $e';
});
debugPrint('β Initialization Error: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Smart FCM Example'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Status: $_status',
style: Theme.of(context).textTheme.titleMedium,
textAlign: TextAlign.center,
),
const SizedBox(height: 20),
if (_token != null) ...[
SelectableText(
'Token:\n$_token',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodySmall,
),
const SizedBox(height: 20),
],
FilledButton.icon(
onPressed: _initializeFCM,
icon: const Icon(Icons.notifications_active),
label: const Text('Initialize FCM'),
),
const SizedBox(height: 10),
const Text(
'Note: Ensure google-services.json (Android) and GoogleService-Info.plist (iOS) are added to the project for this to work.',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.grey, fontSize: 12),
),
],
),
),
),
);
}
}