notification_hub 0.0.7
notification_hub: ^0.0.7 copied to clipboard
An event broadcasting mechanism designed for dispatching notifications to registered observers, inspired by the structure of the iOS Notification Center.
notification_hub #
An event broadcasting mechanism designed for dispatching notifications to registered observers, inspired by the structure of the iOS Notification Center.
Video demo showcasing its usage #
Explanation #
There are three notification channels: Mammals, Insects, and Birds.
Widget Asubscribes to the Mammals notification channel.Widget Bsubscribes to the Insects notification channel.Widget Csubscribes to the Insects notification channel.Widget Dsubscribes to both the Mammals and Birds notification channels.
When a Dog is posted, Widget A and D will receive it.
When Bees are posted, Widget B and C will receive the notification.
If an Owl is posted, only Widget D will receive the notification.
Getting Started #
notification_hub is available through pub.dev.
Add the Dependency #
To use notification_hub in your Flutter project, add the following dependency to your pubspec.yaml file:
dependencies:
...
notification_hub: ^0.0.7
Usage example #
For a detailed usage example, check the example folder.
Subscribe Observer (Subscribe to a single notification channel) #
Create a notification channel (e.g 'Greetings'), subscribe then listen to events.
NotificationHub.instance.addSubscriber(this, notificationChannel: 'Greetings',
onData: (event) {
print("$event");
},
onDone: (message) {
print("$message");
},
onError: (error) {
print(error.toString());
});
Here's a breakdown of each part:
addSubscriber: This method is likely used to subscribe an object (in this case, the current object, represented by this) to a specific notification channel. The this can also be replaced by an instance of an object.
notificationChannel: 'Greetings' Specifies the name of the notification channel to which the object is subscribing. In this case, it's 'Greetings'.
onData: This is a callback function that will be executed when new data is received on the 'Greetings' channel. The event parameter represents the data received.
onDone: This is a callback function that will be executed when the subscription is completed successfully. The message parameter represents any message related to the completion.
onError: This is a callback function that will be executed if an error occurs during the subscription. The error parameter represents the error object.
Subscribe Observer (Subscribe to multiple notificaiton channels) #
NotificationHub.instance.addSubscriber(this, notificationChannel: 'Morning',
onData: (event) {
print("$event");
},
onDone: (message) {
print("$message");
},
onError: (error) {
print(error.toString());
});
NotificationHub.instance.addSubscriber(this, notificationChannel: 'Afternoon',
onData: (event) {
print("$event");
},
onDone: (message) {
print("$message");
},
onError: (error) {
print(error.toString());
});
Unsubscribe #
Unsubscribe by caling removeSubscriber. You can also unsubscribe from a specified notification channel, assuming the object or widget has subscribed to more than one notification channels.
Unsubscribe from All notification channels
NotificationHub.instance.removeSubscriber(object: this);
Unsubscribe from a specified notification channel
NotificationHub.instance.removeSubscriber(object: this, notificationChannel: 'Greetings');
Post notification #
NotificationHub.instance.post(notificationChannel:'Greetings', data: 'Hello');