smart_notification_manager 0.0.3 copy "smart_notification_manager: ^0.0.3" to clipboard
smart_notification_manager: ^0.0.3 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 #

  1. Overview

  2. Setup Instructions

  3. Initialization

  4. Examples

  5. Testing Push Notifications

  6. Issues Solved by This Package


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 #

  1. Activate the package globally:

    dart run global activate smart_notification_manager
    
  2. Set up the package:

    dart run smart_notification_manager:setup
    
  3. 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:

  1. Obtain the server key:

    final serverKey = await ServerKeyGetter.getServerKey(serviceAccountJson);
    
  2. 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 the res/raw folder and matches the channel_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.
12
likes
0
points
79
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for managing local and push notifications with scheduling, custom configurations, and background handling support.

License

unknown (license)

Dependencies

dartz, firebase_messaging, flutter, flutter_local_notifications, flutter_timezone, fluttertoast, googleapis_auth, http, plugin_platform_interface, receive_intent, timezone, workmanager

More

Packages that depend on smart_notification_manager

Packages that implement smart_notification_manager