Package created to use the notification-api Service (https://www.notificationapi.com/) in Laravel projects
Official Doc for Notification Api: https://docs.notificationapi.com/
To get the latest version of notificationapi-laravel
on your project, require it from "composer":
$ composer require nairanomura/notificationapi-laravel
Or you can add it directly in your composer.json file:
{
"require": {
"nairanomura/notificationapi-laravel": "1.0"
}
}
Register the provider directly in your app configuration file config/app.php
:
'providers' => [
// ...
NairanOmura\NotificationApi\NotificationApiServiceProvider::class,
]
The following lines will be auto added in config/services.php
by provider.
return [
//...
'notification-api' => [
'key' => env('NOTIFICATION_API_KEY'),
'secret' => env('NOTIFICATION_API_SECRET'),
]
];
So add the following environment variables in .env file to finish a configuration:
#.env
NOTIFICATION_API_KEY=clientID
NOTIFICATION_API_SECRET=clientSecret
This keys was offered by notification api after register an account
Use Artisan to create a notification:
php artisan make:notification SomeNotification
Return [notification-api]
in the public function via($notifiable)
method of your notification:
public function via($notifiable)
{
return ['notification-api'];
}
Add the method public function toNotificationApi($notifiable)
to your notification:
//...
public function toNotificationApi($notifiable)
{
return [
"notificationId" => "notification_example",
"user" => [
"id" => $notifiable->getAttribute('id'),
"email" => $notifiable->getAttribute('email'),
],
"mergeTags" => [
"userName" => auth()->user()->name,
"clickAction" => config('app.env')."/example"
]
];
}
*Attention: To call notification by the user model, the User model must be "Notifiable" Trait
class User extends Authenticatable
{
use Notifiable;
//...
#notify one user
$user->notify((new SomeNotification(..)));
#notify multiple users
Notification::send($users, new SomeNotification(..);
After install and configure we can use the library calling the helper notification_api
or class NotificationApiService
Example:
$data = [
"notificationId" => "order_tracking",
"user" => [
"id" => "[email protected]",
"email" => "[email protected]",
"number" => "+15005550006"
],
"mergeTags" => [
"item" => "Krabby Patty Burger",
"address" => "124 Conch Street",
"orderId" => "1234567890"
]
];
$result = notification_api($data);
#or
$result = (new NotificationApiService)->send($data)