Smart Notification Manager
Author: Ahmed Wael
Profession: Software Engineer and Flutter Developer Facebook Profile
Table of Contents
Overview
The Smart Notification Manager package simplifies notification management in Flutter apps. It supports:
- Local and push notifications.
- Notifications in the foreground, background, and even when the app is killed.
- Advanced features such as scheduled, periodic, and topic-based notifications.
Setup Instructions
-
Set up the package:
dart run smart_notification_manager:setup
-
Ensure your app’s battery settings allow it to work without restrictions in a killed state.
Initialization
Local Notifications
To initialize local notifications, use the following code in your main.dart
file:
LocalNotificationSetup().initialize(
config: LocalNotificationConfig(
isWorkInBackground: true, // Enables background notifications.
isInDebugMode: true, // Displays debug information during development.
onBgNotifResponse: (details) {}, // Handle background notification response.
onNotifResponse: (details) {}, // Handle notification response when tapped.
onError: () {}, // Handle initialization errors.
onSucess: () {}, // Handle successful initialization.
),
);
Push Notifications
To initialize push notifications:
PushNotificationSetup().initialize(
config: PushNotificationConfig(
backgroundHandler: (message) async {
// Handle background push messages.
},
foregroundHandler: (message) {
// Handle foreground push messages.
},
),
);
Note: By default, foreground notifications trigger a basic local notification, while background notifications are handled by Firebase Messaging.
Examples
Basic Notification
final result = await LocalNotificationSender().sendNotification(
LocalNotificationModel(
isWorkInBackground: true,
notificationType: NotificationType.basic,
),
);
Scheduled Notification
final result = await LocalNotificationSender().sendNotification(
LocalNotificationModel(
isWorkInBackground: true,
notificationType: NotificationType.schedule,
date: Date(year: 2025, month: 5, day: 21, hour: 16, minute: 5),
),
);
Periodic Notification
final result = await LocalNotificationSender().sendNotification(
LocalNotificationModel(
isWorkInBackground: true,
notificationType: NotificationType.peroidic,
repeatInterval: NotifierRepeatInterval.everyMinute,
),
);
Cancel Notifications
await NotificationCancellerImpl().cancelById(
NotifierCancellerModel(
id : 0,
isInBackground : false,
onFailure : (String error) {
print(error);
},
onSuccess : () {
print("Notification Canceled Successfuly");
}
)
);
### Push Notification by Token
```dart
await PushNotificationSender().sendNotification(
PushNotificationModel(
json: _getServiceAccountJson,
projectId: getProjectId,
title: "Hello",
body: "Ahmed",
token: getToken,
type: PushNotificationType.byToken,
),
);
Push Notification by Topic
First, subscribe to the topic:
Topic.subscribeToTopic("all");
Then, send a notification:
await PushNotificationSender().sendNotification(
PushNotificationModel(
json: _getServiceAccountJson,
projectId: getProjectId,
title: "Hello",
body: "Ahmed",
topic: "all",
type: PushNotificationType.byTopic,
),
);
Note: Use
UserToken.getToken()
to retrieve the user token. Obtain the service account JSON and project ID from your Firebase project.
Testing Push Notifications
To test push notifications using Postman:
-
Obtain the server key:
final serverKey = await ServerKeyGetter.getServerKey(serviceAccountJson);
-
Configure Postman:
-
Authorization: Use the
Bearer
token with the server key. -
Body: Use the following JSON structure:
{ "message": { "token": "your_device_token", "notification": { "title": "Sample Title", "body": "Sample Body", "image": "https://example.com/image.jpg" }, "android": { "notification": { "sound": "custom_sound", "channel_id": "channel_id" } } } }
-
Note: Ensure the
sound
file is in theres/raw
folder and matches thechannel_id
in your local notification configuration.
Issues Solved by This Package
- Handles scheduled notifications with correct timezone support.
- Resolves notification sound issues in release builds.
- Supports custom sounds for push notifications.
- Prevents resource removal (e.g., sound files) during release builds.
Libraries
- add_permissions
- add_receiver
- adjust_android_mainfestfile
- adjust_build_gradle_in_android_file
- core/enums/notification_type
- core/errors/failures
- core/models/config/local_notifier_config
- core/models/config/notifier_config
- core/models/config/push_notifier_config
- core/models/notifier_models/date
- core/models/notifier_models/local_notifier_model
- core/models/notifier_models/notifier_canceller_model
- core/models/notifier_models/notifier_model
- core/models/notifier_models/push_notifier_model
- core/models/permissions/permission_model
- core/services/notifier_cancller/notifier_canceller
- core/services/notifier_cancller/notifier_cancller_impl
- core/services/notifier_sender/local_notifier_sender
- core/services/notifier_sender/notifier_sender
- core/services/notifier_sender/push_notifier_sender
- core/services/notifier_setup/local_notifier_setup
- core/services/notifier_setup/notification_setup
- core/services/notifier_setup/push_notifier_setup
- core/utils/api_headers_getter
- core/utils/api_url_getter
- core/utils/basic_notifiy
- core/utils/exact_alarm_permmison_requester
- core/utils/exact_notifications
- core/utils/get_token
- core/utils/local_notification_repo
- core/utils/local_notification_trier
- core/utils/notification_channel_creator
- core/utils/notification_enabled_checker
- core/utils/notification_failure_handler
- core/utils/notification_handler_getter
- core/utils/notification_handlers
- core/utils/notification_permission_requester
- core/utils/notify_details_creator
- core/utils/pending_notifications_requester
- core/utils/perodic_notify
- core/utils/psuh_notification_repo
- core/utils/push_notification_message
- core/utils/push_notification_trier
- core/utils/push_notification_type
- core/utils/push_notifier_permissions
- core/utils/repeat_interval_getter
- core/utils/schedule_notifiy
- core/utils/server_key_getter
- core/utils/style_information_creator
- core/utils/subscribe_to_topic
- core/utils/task
- create_fcm_sound
- create_proguard_file
- ger_receiver
- get_android_mainfest_file
- get_android_mainfest_file_path
- get_permissions
- smart_notification_manager
- update_build_gradle_in_app
- update_compile_sdk
- update_main_activity
- update_raw_folder