Reactive Laravel Notifications

Building advanced Laravel notifications, solving multilingual and content changing problems.

Yassir Awad
Digital Cloud
3 min readJan 15, 2019

--

Laravel notification was released in version 5.3 as a major framework addition. It provides a simple, expressive API for sending notifications across a variety of delivery channels with pre-shipped channels, and as always the Laravel community contributes and offers many custom channels.

One of the essential channels is Database Notification that stores the notification body in the predefined notifications table. Let’s start with a simple example assuming that we have InvitationNotification class that has toArray method containing the notification bodies like title and details:

Bottleneck

As you see in the previous code, Laravel DatabaseChannel will store the result of toArray or toDatabase methods in notification data to be used later, and this may be worked on some projects, but from here we have some limitations:

  • Having multiple languages, and the user can switch between these languages, how can we display title and details once in English and the other in Arabic?
  • What can we do if the notification content changed dynamically? For example, birthday notification “Today is John Doe’s birthday”, after one-day notification required to be “Yesterday was John Doe’s birthday” and so forth.
  • When a user likes a post, a notification is sent to post creator “Jane doe liked your post”, and when another user likes the post, a notification will be changed to “Jon and Jane liked your post”.

To support multilingual, I tried to solve the problem by storing translations keys and their parameters in the database, and when reading notification applies the trans function on title and details. But also there is a gap because the trans parameters maybe also have translatable keys.

For the second point, we can’t make these changes only by creating a new notification or updating a current notification body with new messages.

Solution

After I reached a deadlock to have the best solution to this problem, I found another solution, and I built a package to solve this problem. The solution relies on Overriding DatabaseChannel to store notification data as a serialized notification object instead of storing an array of keys and values.

I also overrode DatabaseNotification model and added an accessor for data field to unserialize data if it was serialized then extract notification object, and finally call toDatabase or toArray methods to get the final output.

Benefits

By using this way, all notification body code preparation are moved into toDatabase or to toArray methods that give your code some of the arrangements.

Usage

Installing

Publish & Migration

Usage

  1. Change trait used in the model from Illuminate\Notifications\Notifiable to Digitalcloud\ReactiveNotification\Traits\Notifiable.

2. Change delivery channel from database or Illuminate\Notifications\Channels\DatabaseChannel to Digitalcloud\ReactiveNotification\Channels\DatabaseChannel.

Examples

  • Translations:
  • Content changed dynamically:

If a member’s birthday is today, the result message will be “Today is John’s birthday, wish him a happy birthday!”

If the member’s birthday was yesterday, the result message will change to “John celebrated his birthday yesterday”.

Finally, if the member’s birthday was on a date, the result message will change to “John celebrated his birthday on 31 Dec”

I hope you enjoyed going through this package and benefited from the details listed. Stay tuned to find out more about other helpful stories in regard to advanced Laravel notifications.

--

--