0 purchases
simple noti
📚️ Introduction: #
Imagine that you are bored at home, so you decide to watch the TV, you turn it on, and a channel X appears.
You watch it for a moment then you decide that you want something funnier, so you change the channel. When you are watching a channel, you are listening to every sound, and observing every event that is being broadcasted on it. On the other hand, when you change the channel, you stop listening and observing.
Now, in other words, let's say that whenever you start watching (or listening to) a channel, you subscribe to it, and whenever you change the channel, you unsubscribe from the previous one, and start listening to the new one.
Moreover, whenever you turn off the TV, you unsubscribe from every channel. Additionally, perhabs in some TVs you may divide the screen into mutliple parts, and start listening to all of them at the same time.
💡 Understanding Pusher #
Well, in terms of Pusher (or sending notifications in general), there is no difference to what we have just explored. You may create a channel where some users subscribe to it and strat listening to every event being broadcasted.
You may create several channels. And every listener may listen to mutliple channels at the same time.
📝 Example #
"But programmatically speaking, how may we differentiate between these stuff?" you may ask. In order to elaborate more on this, let's see the following example:
Assume that we want to create a chat app like Whatsapp, where every user may chat with several users and recieve messages in real time. And a notification should be sent to him whenever someone messages him.
Try to take a moment and think how you would implement this.
Consider the following approach:
We have an important note that every chat has its id, let's call it: chatId.
So, let's say that every time our user (let's call him u) enters a chat, we create a channel called: chat.${chatId} for example, and force the sender and the reciever to subscribe to that channel, so that whenever the sender sends a message, it will be broadcasted to this channel, and as the reciever is listening to this channel, he will be notified that a message has arrived.
Yet, we have a small problem here, which is that whenever an event is triggered (broadcasted) into this channel, every subscriber will be notified including the sender. So, how may we solve this?
We know that every user has a unique id, so we may take advantage of this note.
A simple approach is to check whether the sender.id == u.id (in other words, we are checking whether the sender is ourself or not), if so, then we may drop the event. Otherwise, we do want to keep the event's data.
Kindly, refer to the example code in order to have further understanding.
🔔 Now, let's move on to notifications part #
A simple approach that we create a channel called: global.${u.id} (in other words, we are creating a channel for ourselves), and subscribe to it, and whenever another user sends a message we will trigger an event with destination channel global.${reciever.id}.
So, we will recieve a message (in terms of real time) as we are listening to chat.${chatId}, and a notification as we are listening to global.${u.id}.
Usage #
💻 Implementation: #
Now, let's go through what we have explored so far.
Firstly, we should create an account on Puhser.com, where we will be provided with: key, app_id, secret, and cluster.
The SimpleNoti class is a singleton, that can be instantiated with init(). Then you need to initialize it with a number of configuration options, here is a quick example with a number of callbacks options:
await SimpleNotifications.init(
onTap: onTap,
appKey: YOUR_KEY,
cluster: YOUR_CLUSTER,
appSecret: YOUR_SECRET,
appId: YOUR_APP_ID,
enableLogging: true,
);
copied to clipboard
onTap method is where you handle the action executed when press on a notification, it's an optional method, and it logs the notification's details by default.
enableLogging is a flag that decides whether you want to include the log messages in your terminal or not, it provides useful messages that help you knowing the current state of your app.
〰️ Subscribtion
After that, you should subscribe to a channel in order to start listening to the events
await SimpleNotifications.subscribe(
channelName: 'chat',
roomId: 0,
);
copied to clipboard
the roomId is an optional paramter, if it is not provided, the subscribtion would be established to channelName (chat in this case), otherwise, it would be established to channelName.roomId (chat.0 in this case).
You may override onEvent method in SimpleNotifications.subscribe(), by default it displays a notification on the screen whenever an event is recieved.
📨 Send a remote notification
You may send a notification using:
await SimpleNotifications.sendNotifications(
channelName: 'chat',
roomId: 0,
title: 'some title',
message: 'your body message',
payload: yourPayloadJson,
);
copied to clipboard
where title is the title of the notification that is going to be sent, message is the the message that is going to be included in the body of the notification, and payload is additional paramter if you want to include a specific data in the event that is going to be triggered.
📩 Send a local notification
You may also send a local notification using
await SimpleNotifications.showNotifications(
title: 'some title',
message: 'your body message',
);
copied to clipboard
🚫 Unsubscribtion and closing the connection
After you have finished with the channel, do not forget to unsubcribe and close the connection in order to free the resources
await SimpleNotifications.unsubscribeAndClose(
channelName: 'chat',
roomId: 0,
);
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.