flutter_firebase_push_notification_provider 1.0.0
flutter_firebase_push_notification_provider: ^1.0.0 copied to clipboard
A Flutter package that provides a unified interface for Firebase Cloud Messaging (FCM) push notifications. Handles permissions, token management, and message routing across all app states.
Flutter Firebase Push Notification Provider #
A Flutter package that provides a unified interface for Firebase Cloud Messaging (FCM) push notifications on iOS and Android. Handles permissions, token management, and message routing across all app states (foreground, background, and terminated).
Features #
- Automatic notification handling in foreground, background, and terminated states
- Unified stream for receiving all notifications
- Automatic permission management
- FCM token refresh handling
- Memory leak prevention with proper subscription management
- Multiple initialization protection
- Singleton pattern for consistent state
Getting Started #
Prerequisites #
- Flutter SDK >= 3.10.0
- Dart SDK >= 3.3.2
- Firebase configured in your project (Setup Guide)
Installation #
Add the package to your pubspec.yaml:
dependencies:
flutter_firebase_push_notification_provider: ^1.0.0
Then run:
flutter pub get
Usage #
1. Initialize in main.dart #
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_firebase_push_notification_provider/flutter_firebase_push_notification_provider.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize Firebase first
await Firebase.initializeApp();
// Initialize the notification provider (Singleton)
final notificationProvider = FlutterFirebasePushNotificationProvider();
await notificationProvider.initialize();
runApp(MyApp());
}
2. Listen to messages #
class _MyWidgetState extends State<MyWidget> {
StreamSubscription<Map<String, dynamic>>? _subscription;
late final FlutterFirebasePushNotificationProvider _notificationProvider;
@override
void initState() {
super.initState();
_notificationProvider = FlutterFirebasePushNotificationProvider();
if (_notificationProvider.isInitialized) {
_subscription = _notificationProvider.messagesStream.listen((message) {
print('Notification received: $message');
// Handle notification
});
}
}
@override
void dispose() {
_subscription?.cancel();
super.dispose();
}
}
3. Get FCM Token #
final provider = FlutterFirebasePushNotificationProvider();
// Synchronous access to current token
final currentToken = provider.currentToken;
// Async method to fetch from Firebase
final token = await provider.getToken();
4. Check permissions #
final provider = FlutterFirebasePushNotificationProvider();
if (provider.hasPermission) {
print('Notifications enabled');
} else {
print('Notifications disabled');
}
Message Format #
Messages received in the stream have the following format:
{
'senderId': String?,
'category': String?,
'collapseKey': String?,
'contentAvailable': bool,
'data': Map<String, dynamic>,
'from': String?,
'messageId': String?,
'messageType': String?,
'mutableContent': bool,
'notification': {
'title': String?,
'body': String?,
'android': {...},
'apple': {...}
},
'sentTime': int?,
'threadId': String?,
'ttl': int?
}
Platform Support #
| Platform | Support |
|---|---|
| Android | ✅ |
| iOS | ✅ |
License #
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.