Beacon is a versatile and efficient notification handling system for both remote and local notifications in your application. It streamlines the process of managing notifications by providing a clear structure and customizable implementation. In this README, you will learn how to use Beacon in your project.
- Create a configuration file called
notification.dart
in your project'slib/config/
directory. - Add your notification providers and channels in the
NotificationConfig
class, as shown in the following example:
class NotificationConfig {
static var providers = {
'push_notification': {
'driver': 'push_notification',
'class': PushNotificationsProviders,
'channels': [
{
'name': 'firebase_messaging',
'class': FirebaseMessagingNotificationProvider,
}
],
},
// Add more providers as needed.
};
}
- For each type of notification you want to handle, create a new class that extends
PushNotificationProvider
orServerNotificationProvider
. - Implement the required methods for each notification provider, as shown in the following example:
class FirebaseMessagingNotificationProvider extends PushNotificationProvider {
final FirebaseMessaging _firebaseMessaging;
FirebaseMessagingNotificationProvider(this._firebaseMessaging);
// Implement the required methods.
}
To allow Beacon to handle push notifications, each notification must have a key called "code". Beacon will search for a suitable PushNotification
subclass that can handle this notification. The PushNotification
subclass must define the code it can handle, the onNotification
method, and, if the PushNotification is
- In your feature service provider, register each push notification by adding an entry in the
pushNotifications
list - Here's an example of registering a PushNotification:
class OrderServiceProvider extends ServiceProvider
with PushNotificationServiceProviderMixin {
@override
Future<void> register(GetIt it) async {
// Register dependencies.
}
@override
List<PushNotification> get pushNotifications => [
OrderUpdatedPushNotifications(),
];
}
create new file for push notifcation , implement the onNotification
and onNotificationTapped
methods to handle the desired actions when a notification is received or tapped.
class OrderUpdatedPushNotifications extends PushNotification
with ActionablePushNotification {
@override
List<String> get code => ['order_placed', 'order_update_delivery_status'];
@override
void onNotification(NotificationData data) {
// Handle notification received.
}
@override
void onNotificationTapped(NotificationData data) {
// Handle notification tapped.
}
}
Beacon comes with predefined notification providers:
- FirebaseMessagingNotificationProvider: A push notifications provider using Firebase Cloud Messaging.
- NotificationCenterProvider: A server notification provider that stores and manages server-side notifications.
- (Optional) NotificationCenterPage: A UI component that reads and renders notifications from the NotificationCenterProvider. It also provides functionality like marking notifications as read.
Beacon allows you to create custom notification providers, such as a OneSignal push notification provider. To create a custom provider, follow the steps in the "Creating Notification Providers" section.
Beacon is still in beta version, and plans to handle the following features in future releases:
- Channel & Category Management
- Quick Actions
- Styled Notifications
- Trigger Notifications