Jump to content

How to use SystemNotifications module?


pwFoo
 Share

Recommended Posts

Soma's right this is still in development, so things may change and it's too early to write up docs. But here's the basic concepts that probably won't change: 

Display notification on next request (these work whether SystemNotification is installed or not, but the output is of course different): 

$this->message("Message to display"); 
$this->warning("Warning to display");
$this->error("Error to display"); 

If outside of an object context, you'd replace $this with wire().

These notifications (above) have been with ProcessWire since the beginning (except for the warning one). The SystemNotifications module continues the same strategy and extends it. It enables notifications that go beyond just simple "display once on next request" notifications. It also enables you to send notifications to any user, regardless of whether they are logged in (they'll get the notifications when they next login). If the user happens to be logged in, they'll get it immediately–it will pop up on their screen without them even having to load a page.

To use these persistent notifications you have to send them to a user ($user = any user): 

$user->notifications->message("Message to send"); 
$user->notifications->warning("Warning to send"); 
$user->notifications->error("Error to send"); 
$user->notifications->save(); // this is required to commit them

They also have a Javascript API, for sending immediate notifications to the current user. These particular notifications do not persist across requests, so they are more like a JS-version of PW's "display once" notifications, except that they display immediately. 

Notifications.message("Message to send");
Notifications.warning("Warning to send");
Notifications.error("Error to send"); 

There's actually a lot more to it, as each notification can have various different flags and properties. Notifications that persist across requests can also be modified, which is great for things like progress bars or other kinds of state changes. Further, they can contain any amount of "details" content, even entire HTML blocks. This is all stuff we'll cover once these are finalized. 

  • Like 1
Link to comment
Share on other sites

Hi Ryan,

thanks for your answer :)

Notification demos work fine, but how I have to initialize the module to work in the frontend?

Add notifications to the admin works fine.

$notify = $users->get('<username>');
$notify->notifications->message("Just a test!");
$notify->notifications->save();

But haven't found out how to add the ajax messages to the frontend too.

It' very interesting and a really nice feature. Great work! :)

Link to comment
Share on other sites

Notification demos work fine, but how I have to initialize the module to work in the frontend?

The notifications are part of the admin. PW doesn't get involved with your front-end output, so it's not attempting to provide anything with regard to notifications on the front-end. However, you can still use the API to add notifications to any user, but they'll need to go to the admin before they will see them, unless you provide your own output code on your front-end. 

If you wanted to output notifications on the front-end of your site, it would actually be as simple as this:

foreach($user->notifications as $no) {
  echo "<p>$no</p>";
}

There are of course several other properties, methods and flags that you can access from the Notification ($no) object. But outputting a notification is basically just a matter of iterating through the $user->notifications field and outputting them all, or just the ones you want. So in this sense, it actually would be quite easy to utilize on the front-end, but like with anything in PW on the front-end, you have to decide how you want the output to be handled. 

At the moment it's seem to be ajax polling notification. Is additional a websocket connection planned / possible?

Currently it's using the same method as the forum we're typing in (IP.Board). I have not kept up with websockets technology lately, but was under the impression we weren't there yet unless you had specific client-side and server-side libraries to handle it and provide the fallbacks, or were using node.js, etc. I'd gladly implement websockets in the future if they enable us to accomplish the same thing more efficiently, easily and reliably without bloat. Last I researched this, we weren't there yet, at least in our LAMP context. But you likely know more than I do about that technology at this point, so interested in hearing more. 

  • Like 3
Link to comment
Share on other sites

Hi Ryan,

thanks. 

Websockets sounds interesting, but You're could be right to wait until it's more effiecient and easy. So may be it's a future option. If I do some tests I'll report it here, but don't know if I have the time to do it ;)

To use php for notifications is fine, but also would love to use ajax notifications in the frontend. First tests no javascript was load by the notifiactions module.

Notifications.message("Message to send");
Notifications.warning("Warning to send");
Notifications.error("Error to send");  

So I'll wait for the js api documentation to see how it work. 

Link to comment
Share on other sites

@pwFoo:

WebSockets are an overkill for something like this - they are designed for distributed messaging between multiple concurrent users for web applications that work in real-time.

For this same reason, they require a special server designed for the purpose - the best currently being something running on top of node.js (like socket.io), a language which was designed for things like this from the ground up. Erlang would be another choice (it even has a socket.io port). In other words, you cannot handle WebSockets with just Apache and mod_php5. Ok, you could always run a PHP-based WS daemon like Ratchet, in the background, but I'm personally not a fan of PHP-based TCP-daemons. They are good for toying with new concepts though. And yes, I do know about the guy who wrote an SSH2 server with PHP, but still, you and I both probably stick with OpenSSH ;)

I don't even see this happening for quite some time because Apache was never designed for this kind of purpose. Neither was PHP.

  • Like 1
Link to comment
Share on other sites

I found php-websockets and would do some tests...

https://github.com/ghedipunk/PHP-Websockets

It doesn't matter which PHP-based project you use because the concept stays the same - you still need something running in the background of the server, listening on a socket. If you really want to play using PHP, just use Ratchet. It's a lot more mature (meaning fully documented for an example).

Link to comment
Share on other sites

  • 7 months later...

Hi Ryan,

any plans in the near future with the notifications module? I plan to use it for simple (ajax) user notifications in the frontend. 

So I would build a process module and some JS code to use ajax user notifications based on SystemNotifications.

$user->notifications

Regards

  • Like 1
Link to comment
Share on other sites

  • 8 months later...

I'd like to add FB-like messages and notifications to PW admin menu. FB-like means:

- admin frontend: js script that each min sends ajax requests to the server and depending on response, updates admin menu item element of DOM. Messages and notifications behave similarly to what we have at FB.

- backend: script(s) that processes ajax request and return notifications data. Notifications data is stored in user template.

Just to give you an idea re notifications, few examples of notifications that gets some user with "sales" role:

- System Task: Prepare quote for order 23451

- System Task: Callback to user 54321 at 12pm

- System Info: Order 98567 has been cancelled

- User Message: Hey, bro can't find you contact. Please call me.

- etc...

Qs:

1. what would be a "right" way to add such "non-PW" menu item to admin menu? Hook during admin menu rendering?

2. there is pre-installed notification field in user account. I'm interested to understand if I can use it w/o custom coding OR extend existing core classes? Where to start digging? or where I can read more about it?

Link to comment
Share on other sites

  • 6 months later...
2 minutes ago, adrian said:

But what if it's not installed? I am guessing you will revert to a popup? I am sure you won't, but please don't automatically install it :)

Just figured I'd use it if it's installed... In this case, it's the jumplink state toggler (enable/disable), so the notification is a nice-to-have, but won't show if Notifications is not installed.

  • Like 1
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...