Skip to content

Sending Notifications

Eric Pfeiffer edited this page May 8, 2016 · 15 revisions

##Sending Notifications##

Sending notifications with Notifynder is very straightforward.

php artisan notifynder:create:category "hello" "world"
$user_sender_id   = 1; // User sender
$user_receiver_id = 2; // User that receive the notification

Notifynder::category('hello')
           ->from($user_sender_id)
           ->to($user_receiver_id)
           ->url('http://localhost')
           ->send();

// we sent the notification!
// now let's check it

$userNotified = User::find($user_receiver_id);

// Return Illuminate\Database\Eloquent\Collection 
// with the notifications in it

dd($userNotified->getNotificationsNotRead());

Now let's review the required information needed to send a successful notification.

###Required information###

  • category('namecategory'): is the category name of wished context to send.
  • to($notifable_type,$notifable_id): It determine who will receive the notification, the first parameter can be only the ID of the notifable entity or the entity classname first, then the id for specify the model (to use when notify to multiple models).
  • from($notifable_type,$notifable_id): It same as the to() method but it determinate the entity who send the notification
  • url('http://yoururl.com): The url parameter is the actual url that you want redirect the user in the UI.

###Extra Replacement Paramaters### On top, there is another optional method that will help you to construct a readable and dynamic sentence to your notification, is the case of extra:

php artisan notifynder:create:category "sayhello" "hello good {extra.period_day}"
$period_day = (is_morning()) ? 'morning' : 'evening';

$notifynder->category('sayhello')
           ->from(1)
           ->to(2)
           ->url('http://localhost')
           ->extra(compact('period_day'))
           ->send();

Response: 'hello good morning/evening' (one of them dynamically depending from the value passed in the extra method).

Tip: You can have multiple {extra.*} values to replace them dynamically.

###Building Advanced Notifications###

Notifynder has multiple way to help you to build your notification, for example:

Let's say that you are building an algorithm that you think will help to manage a section of notifications and you'll be more happy to work with an array or other ways for build the notification for example you can do:

Method 1 (Builder)

$this->notifynder->category('sayhello')
           ->from(1)
           ->to(2)
           ->url('http://localhost')
           ->extra(compact('period_day'));

Method 2 (Array)

$notifynder = $this->notifynder;

$notifynder['category'] = 'sayhello';
$notifynder['to'] = 20;
$notifynder['from'] = 23;
$notifynder['extra'] = compact('period_day');

$notifynder->send();

Method 3 (Properties)

$notifynder = $this->notifynder;

$notifynder->category = 'sayhello';
$notifynder->to = 20;
$notifynder->from = 23;
$notifynder->extra = compact('period_day');

$notifynder->send()

The preference might be the builder, but feel free to use the other as you wish.

Building Multiple Notifications####

Notifynder offer the availability to send multiple notifications at once, running 1 query, it make easy to build it as the following:

// It send a notification to all the users 
try {
    $this->notifynder->loop($users, function(NotifynderBuilder $builder, $user) {

       $builder->category('sayhello')
           ->from(1)
           ->to($user->id)
           ->url('http://localhost')
           ->extra(compact('period_day')); 

    })->send();
} catch (EntityNotIterableException $e) {
} catch (IterableIsEmptyException $e) {
}

Next: Extend Senders

Clone this wiki locally