notification_hub 0.0.3
notification_hub: ^0.0.3 copied to clipboard
An event broadcasting mechanism designed for dispatching notifications to registered observers, inspired by the structure of the iOS Notification Center.
notification_center #
An event broadcasting mechanism designed for dispatching notifications to registered observers, inspired by the structure of the iOS Notification Center.
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.1
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(notificationName:'Greetings', data: 'Hello');