Abdullah8888 / notification_hub

An event broadcasting mechanism designed for dispatching notifications to registered subscribers, inspired by the structure of the iOS Notification Center.

Home Page:https://learnwithtunde.hashnode.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

notification_hub

An event broadcasting mechanism designed for dispatching notifications to registered subscribers, inspired by the structure of the iOS Notification Center.

Video demo showcasing its usage

Video demo

Explanation

There are three notification channels: Mammals, Insects, and Birds.

  • Widget A subscribes to the Mammals notification channel.
  • Widget B subscribes to the Insects notification channel.
  • Widget C subscribes to the Insects notification channel.
  • Widget D subscribes 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.9

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');

About

An event broadcasting mechanism designed for dispatching notifications to registered subscribers, inspired by the structure of the iOS Notification Center.

https://learnwithtunde.hashnode.dev/

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:C++ 33.0%Language:Dart 31.8%Language:CMake 27.8%Language:HTML 2.6%Language:Swift 2.5%Language:C 2.0%Language:Kotlin 0.2%Language:Objective-C 0.1%