Skip to content

NairanOmura/notificationapi-laravel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Notification-api for Laravel

Package created to use the notification-api Service (https://www.notificationapi.com/) in Laravel projects

Official Doc for Notification Api: https://docs.notificationapi.com/

Installation

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"
    }
}

Config

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

Using

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"
        ]
    ];
}

example of notification send

*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(..);

Extra: Helpers

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)

About

Package to use notification api (https://www.notificationapi.com) service in Laravel projects

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages