Masamune logo

Masamune Notification

Follow on GitHub Follow on X Follow on YouTube Maintained with Melos

GitHub Sponsor


[GitHub](https://github.com/mathrunet) | [YouTube](https://www.youtube.com/c/mathrunetchannel) | [Packages](https://pub.flutter-io.cn/publishers/mathru.net/packages) | [X](https://x.com/mathru) | [LinkedIn](https://www.linkedin.com/in/mathrunet/) | [mathru.net](https://mathru.net)


Masamune Notification

Overview

masamune_notification is the base package for notification functionality in Masamune apps. It provides:

  • Abstract notification controllers (RemoteNotification, LocalNotification)
  • Notification models for scheduling and persistence
  • Functions actions for backend integration
  • Runtime adapters for testing

Note: This is the base package. You'll also need concrete implementations:

  • masamune_notification_firebase for Firebase Cloud Messaging (push notifications)
  • masamune_notification_local for local notifications

Usage

Installation

flutter pub add masamune_notification
flutter pub add masamune_notification_firebase  # For remote push notifications
flutter pub add masamune_notification_local     # For local notifications

Concrete Implementations

This base package provides the interfaces. Use concrete implementations for actual functionality:

Firebase Cloud Messaging (masamune_notification_firebase)

  • Provides FirebaseRemoteNotificationMasamuneAdapter
  • Handles push notifications from Firebase
  • See masamune_notification_firebase package for setup

Local Notifications (masamune_notification_local)

  • Provides MobileLocalNotificationMasamuneAdapter
  • Schedules local notifications on device
  • See masamune_notification_local package for setup

Runtime Adapters (Testing)

  • RuntimeRemoteNotificationMasamuneAdapter - Mock remote notifications
  • RuntimeLocalNotificationMasamuneAdapter - Mock local notifications

Notification Controllers

The package provides two main controllers:

RemoteNotification Controller:

  • Handles push notifications (requires implementation adapter)
  • Get FCM tokens
  • Subscribe to topics
  • Handle foreground/background messages

LocalNotification Controller:

  • Schedules local notifications (requires implementation adapter)
  • Request permissions
  • Show immediate notifications
  • Cancel scheduled notifications

Notification Models

LocalNotificationScheduleModel: Persist local notification schedules

final schedule = LocalNotificationScheduleModel(
  localNotificationScheduleId: "reminder-1001",
  notificationId: 1001,
  title: "Daily Reminder",
  body: "Don't forget to exercise!",
  scheduledAt: DateTime.now().add(Duration(days: 1)),
  repeatSettings: LocalNotificationRepeatSettings.daily(),
);

// Save to database
final doc = ref.app.model(
  LocalNotificationScheduleModel.collection(),
).create();
await doc.save(schedule);

RemoteNotificationScheduleModel: Schedule remote push notifications via backend

final remoteSchedule = RemoteNotificationScheduleModel(
  remoteNotificationScheduleId: "promo-001",
  title: "Special Offer",
  body: "Check out our new features!",
  scheduledAt: DateTime.now().add(Duration(hours: 2)),
  target: NotificationTarget.topic("all-users"),
);

await remoteSchedule.save();

Functions Actions

SendRemoteNotificationFunctionsAction: Send push notifications from backend

final functions = ref.app.functions();

await functions.execute(
  SendRemoteNotificationFunctionsAction(
    title: "Breaking News",
    body: "Important update available.",
    token: userToken,  // or use topic/condition
    data: {"articleId": "abc123"},
  ),
);

For Implementation Details

See the implementation-specific packages:

  • masamune_notification_firebase - Firebase Cloud Messaging setup and usage
  • masamune_notification_local - Local notification scheduling and display

GitHub Sponsors

Sponsors are always welcome. Thank you for your support!

https://github.com/sponsors/mathrunet

Libraries

masamune_notification
Masamune plugin to use base classes and stubs to implement PUSH notifications.
models/local_notification_schedule
models/remote_notification_schedule