notification_history 1.0.3
notification_history: ^1.0.3 copied to clipboard
A Flutter package to manage and display the history of notifications received by the app, including Firebase Cloud Messaging (FCM) notifications.This package provides a local database to store notific [...]
Notification History #
A Flutter package for managing and displaying push notification history using Firebase Cloud Messaging (FCM) and local notifications.
Features #
Store notifications in a local SQLite database. Display notification history with read/unread status. Support for FCM and local notifications. Clean architecture with dependency injection using get_it.
Installation #
- Add the package to your pubspec.yaml:
dependencies:
notification_history:
path: /path/to/notification_history # For local development
# Or use: git: https://github.com/AlbertStany/PushNotificationHistoryPackageFlutter
# Or use: version: ^1.0.2
- Run flutter pub get.
Setup #
- Configure Firebase:
Add google-services.json to android/app/ and GoogleService-Info.plist to ios/Runner/. Initialize Firebase in your app:
await Firebase.initializeApp();
- Set Up Dependency Injection:
import 'package:get_it/get_it.dart';
import 'package:notification_history/notification_history.dart';
final getIt = GetIt.instance;
void setupDI() {
getIt.registerSingleton<NotificationRepository>(SqfliteNotificationRepository.instance);
getIt.registerSingleton<AddNotification>(AddNotification(getIt<NotificationRepository>()));
getIt.registerSingleton<GetNotifications>(GetNotifications(getIt<NotificationRepository>()));
getIt.registerSingleton<MarkNotificationAsRead>(MarkNotificationAsRead(getIt<NotificationRepository>()));
getIt.registerSingleton<DeleteNotification>(DeleteNotification(getIt<NotificationRepository>()));
getIt.registerSingleton<MarkAllNotificationsAsRead>(MarkAllNotificationsAsRead(getIt<NotificationRepository>()));
getIt.registerSingleton<NotificationHandler>(
NotificationHandler._init(
addNotification: getIt<AddNotification>(),
channelId: 'your_channel_id',
channelName: 'Your Channel Name',
androidIcon: '@mipmap/your_icon',
),
);
}
- Initialize Notifications:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
setupDI();
await getIt<NotificationHandler>().initialize();
runApp(MyApp());
}
- Display Notification History:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NotificationHistoryScreen(
getNotifications: getIt<GetNotifications>(),
markNotificationAsRead: getIt<MarkNotificationAsRead>(),
deleteNotification: getIt<DeleteNotification>(),
markAllNotificationsAsRead: getIt<MarkAllNotificationsAsRead>(),
),
),
);
- Show a Local Notification:
getIt<NotificationHandler>().showNotification(
'Test Title',
'Test Body',
payload: 'test_payload',
);
- Android Configuration
Add to android/app/src/main/AndroidManifest.xml:
<application ...>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="your_channel_id" />
</application>
Add to android/app/src/main/res/values/strings.xml:
<resources>
<string name="default_notification_channel_name">Your Channel Name</string>
</resources>
- iOS Configuration
Enable Push Notifications in Xcode. Add GoogleService-Info.plist to ios/Runner/.
Example #
See the example/ directory for a sample app.
License #
MIT License