smart_notification_manager 0.0.7
smart_notification_manager: ^0.0.7 copied to clipboard
A Flutter plugin for managing local and push notifications with scheduling, custom configurations, and background handling support.
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 #
-
Activate the package globally:
dart run global activate smart_notification_manager
-
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:
LocalNotifierSetup().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.
),
).fold(
(failure) => print("Initialization failed: $failure"),
(success) => print("Initialization succeeded!"),
);
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,
),
);
Push Notification by Token #
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.