Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Feb 25 2019 17:22
    EOM opened #35
  • Dec 20 2018 22:58
    JeanValJeann opened #34
  • Oct 07 2018 15:03
    Devin0231577 commented #31
  • Oct 07 2018 14:15
    rafi01010010 commented #31
  • Sep 18 2018 03:50
    tgoeminne opened #33
  • Sep 16 2018 15:04
    emiliano-idata opened #32
  • Jun 18 2018 03:05
    Devin0231577 opened #31
  • Jun 13 2018 14:38
    zakblacki edited #30
  • Jun 13 2018 14:38
    zakblacki opened #30
  • May 23 2018 11:53
    LubosRemplik commented #29
  • Mar 02 2018 13:11
    codecov-io commented #29
  • Mar 02 2018 13:10
    codecov-io commented #29
  • Mar 02 2018 13:09
    codecov-io commented #29
  • Mar 02 2018 13:08
    codecov-io commented #29
  • Mar 02 2018 13:07
    codecov-io commented #29
  • Mar 02 2018 13:07
    codecov-io commented #29
  • Mar 02 2018 13:07
    LubosRemplik commented #29
  • Mar 02 2018 13:06
    codecov-io commented #29
  • Mar 02 2018 13:06
    codecov-io commented #29
  • Mar 02 2018 12:44
    codecov-io commented #29
Bob Mulder
@bobmulder
I will send you a code sample as soon as possible @akkaweb
Bob Mulder
@bobmulder

@akkaweb This is an example how to work with events to make your plugin work with the notifier.

First of all: read more about events here: http://book.cakephp.org/3.0/en/core-libraries/events.html

This example shows you what to do in your model. Lets call your event in your case 'Model.Feedback.afterSave'. This is the only change you should do at the side of your plugin. Whats done now, is that when a feedback item has been sent, the event is called.

So what we got to do now is use the event to notify.

For that we need a listener. If you see that example, it should be clear enough you have to catch your event, and use the Notifier...

AKKAWEB
@akkaweb
@bobmulder thank you for that. I will try and implement that within my plugin. I will post back with how successful I get with it. In true, I had read about events in the book several times, but I always left with some confusion. But I think now, I understand the separation between the event itself and listeners. Thanks for that!
AKKAWEB
@akkaweb
@bobmulder Because I wanted to attach notifications to other plugins that are not in my control and I could not change their code, I researched a little more and I found the following solution
Because CakePHP already has an afterSaveevent that is fired by the Model, all I had to do was create src/Event/FeedbackListener.php and look into the Fired Model Event for the Feedback Save Event
Bob Mulder
@bobmulder
Did you manage to do that @akkaweb? I doubt because the afterSave event has been fired only to that class, right?
AKKAWEB
@akkaweb
Here is the code that I used in my FeedbackListener.php
This message was deleted
Bob Mulder
@bobmulder
Haha please, use markdown :P
AKKAWEB
@akkaweb
Ok... let me try
<?php

namespace App\Event;

use Cake\Event\EventListenerInterface;
use Notifier\Utility\NotificationManager;

class FeedbackListener implements EventListenerInterface {

  public function implementedEvents() {
    return [
        'Model.afterSave' => 'addFeedbackNotification',
    ];
  }

  public function addFeedbackNotification($event, $entity) {
    if ($entity->source() == 'AkkaFeedback.Feedbacks' && $entity->isNew()) {
      // Add notification
      $notificationManager = NotificationManager::instance();
      $notificationManager->addTemplate('newMessage', [
          'title' => 'New Message Notification',
          'body' => 'New message from :name'
      ]);
      $notificationManager->addRecipientList('administrators', [7]);
      $notificationManager->notify([
          'recipientLists' => ['administrators'],
          'template' => 'newMessage',
          'vars' => [
              'id' => $entity->get('id'),
              'name' => $entity->get('name'),
              'feedback' => $entity->get('feedback')
          ]
      ]);
    }
  }
}
Plus I added this to bootstrap.php
/**
 * Event listeners
 */
use App\Event\FeedbackListener;
use Cake\Event\EventManager;

$feedackListener = new FeedbackListener();
EventManager::instance()->attach($feedackListener);
Bob Mulder
@bobmulder
Yeah, in fact this should work. However, using this code will be very expensive, because you listen to the event Model.afterSave. So, on every model's safe you'll fire this event (thats why you used if/else). So to improve your code I would suggest creating a new event into your afterSave-method in your model (in the plugin), and listen on that event. The bootstrap looks good!
AKKAWEB
@akkaweb
I will definitely add afterSave to my plugin to make it easy to track its save events. The reason I went this route is because I use a plugin from another developer that does not fire any specific events and I wanted a notification on new creation as well. But all good for now, thanks for the help!
Bob Mulder
@bobmulder
No problem! Good luck and let me know if you need help!

Oh, to improve your code, I would move

$notificationManager->addTemplate('newMessage', [
          'title' => 'New Message Notification',
          'body' => 'New message from :name'
      ]);

to your bootstrap.php. Else the templates can't be read when showing them...

AKKAWEB
@akkaweb
Much better! Thanks a lot! I also moved
$notificationManager->addRecipientList('administrators', [7]);
as I will be using administrators in a bunch of other places. Code look much more cleaner!
Bob Mulder
@bobmulder
Nice! Thats what it's meant to. I understand it wasn't clear enough in the docs?
AKKAWEB
@akkaweb
Well! The doc was not as specific as you have put it to me now. Now it makes way more sense. I think it would help other users of your plugin if you can make this suggestion there...
Just to add, everything else though is very clear on your doc and easy to follow..
AKKAWEB
@akkaweb
@bobmulder Perhaps on your next release. It would be nice to have an option within getNotifications() that would limit the amount of notifications to return. Like latest 4 notifications.
Bob Mulder
@bobmulder
Great idea @akkaweb, as well, feel free to do a PR ;) Don't have that much time for this plugin...
AKKAWEB
@akkaweb
@bobmulder Will do! Thanks!
AKKAWEB
@akkaweb
@bobmulder FYI! I did a PR for the LIMIT change. Once you get a chance to review, please let me know if you have any questions.
Bob Mulder
@bobmulder
@akkaweb Thank you! I've added a comment to #8
AKKAWEB
@akkaweb
@bobmulder Thanks for the constructive comments. I have made suggested changes and updated submitted code.
designskiller
@designskiller
hi
How to install notifier without using composer
Bob Mulder
@bobmulder
Why should you want that @designskiller?
designskiller
@designskiller
because if i do that with composer others will be updated too
Am using an existing project.
Bob Mulder
@bobmulder
Thats only positive right?
designskiller
@designskiller
yes
Bob Mulder
@bobmulder
You can download a zip from github...
But I strongly recommend using composer
designskiller
@designskiller
is there any option with composer that only install notifier without updating others ?
Bob Mulder
@bobmulder
Add the requirement manually to the composer.json and run 'composer update (package)'
designskiller
@designskiller
Thanks @bobmulder
Bob Mulder
@bobmulder
To improve you work-flow you should define versions in composer. That makes it easier to rely on previous versions to prevent bugs, but still use composer the easy way
designskiller
@designskiller
Thanks @bobmulder i will improve
Bob Mulder
@bobmulder
No problem @designskiller, you're welcome!
designskiller
@designskiller
Hi
what is the best way to check notification every second ?
Bob Mulder
@bobmulder
Doubt if you should do that. But if you want to; create an json endpoint which will check for notifications....
designskiller
@designskiller
Why you doubt that @bobmulder
Bob Mulder
@bobmulder
Every second a query with many users at the same time will be heavy...
designskiller
@designskiller
then what shuold i do any ideas @bobmulder
Bob Mulder
@bobmulder
Check per minute @designskiller ?
AKKAWEB
@akkaweb
@designskiller, @bobmulder is correct as it can be heavy on the server. I had the same need to check quickly for new notifications at page load. The way that it works for me is that I start checking immediately at page load, but my jquery script increments the call by 1 second after each call. Such that I get every second, every two seconds, every three seconds, etc. You can even increment by the X seconds you desire or double the interval. Like every second, two seconds, four seconds, eight seconds.