Browse Source

add some event, listener and fsm channel

remotes/origin/mohammad
mahdihty 4 years ago
parent
commit
01c5ce3e38
  1. 23
      app/Channels/FcmChannel.php
  2. 132
      app/Channels/Messages/FcmMessage.php
  3. 6
      app/Enums/cruds.php
  4. 38
      app/Events/BusinessUserCreate.php
  5. 46
      app/Listeners/BusinessUserCreateNotif.php
  6. 32
      app/Models/ReportableRelation.php
  7. 10
      app/Models/User.php
  8. 63
      app/Notifications/DBNotification.php
  9. 48
      app/Notifications/FcmNotification.php
  10. 2
      app/Notifications/MailNotification.php
  11. 9
      app/Providers/AppServiceProvider.php
  12. 7
      app/Providers/EventServiceProvider.php
  13. 35
      database/migrations/2021_03_08_114700_create_notifications_table.php
  14. 4
      resources/lang/fa/notification.php

23
app/Channels/FcmChannel.php

@ -0,0 +1,23 @@
<?php
namespace App\Channels;
use Illuminate\Notifications\Notification;
class FcmChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
$message = $notification->toFcm($notifiable);
$recipients = $notifiable->routeNotificationFor('fcm', $notification);
}
}

132
app/Channels/Messages/FcmMessage.php

@ -0,0 +1,132 @@
<?php
namespace App\Channels\Messages;
class FcmMessage
{
/**
* The devices token to send the message from.
*
* @var array|string
*/
public $to;
/**
* The topic of the FCM message.
*
* @var array
*/
public $topic;
/**
* The data of the FCM message.
*
* @var array
*/
public $data;
/**
* The notification body of the FCM message.
*
* @var array
*/
public $notification;
/**
* The condition for receive the FCM message.
*
* @var array
*/
public $condition;
/**
* The priority of the FCM message.
*
* @var string
*/
public $priority = 'normal';
/**
* Set the devices token to send the message from.
*
* @param array|string $to
* @return $this
*/
public function to($to)
{
if (is_array($to) && count($to) === 1) {
$this->to = $to[0];
} else {
$this->to = $to;
}
return $this;
}
/**
* Set the topic of the FCM message.
*
* @param string $topic
* @return $this
*/
public function topic(string $topic)
{
$this->topic = $topic;
return $this;
}
/**
* Set the data of the FCM message.
*
* @param array $data
* @return $this
*/
public function data(array $data)
{
$this->data = $data;
return $this;
}
/**
* Set the notification of the FCM message.
*
* @param array $notification
* @return $this
*/
public function notification(array $notification)
{
$this->notification = $notification;
return $this;
}
/**
* Set the condition for receive the FCM message.
*
* @param string $condition
* @return $this
*/
public function condition(string $condition)
{
$this->condition = $condition;
return $this;
}
/**
* Set the priority of the FCM message.
*
* @param string $priority
* @return $this
*/
public function priority(string $priority)
{
$this->priority = $priority;
return $this;
}
}

6
app/Enums/cruds.php

@ -3,9 +3,9 @@
return [
'inverse' => [
10 => ['name' => 'Create'],
20 => ['name' => 'Update'],
30 => ['name' => 'Delete'],
10 => ['name' => 'Create', 'singular_name' => 'create'],
20 => ['name' => 'Update', 'singular_name' => 'update'],
30 => ['name' => 'Delete', 'singular_name' => 'delete'],
],
];

38
app/Events/BusinessUserCreate.php

@ -0,0 +1,38 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class BusinessUserCreate
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

46
app/Listeners/BusinessUserCreateNotif.php

@ -0,0 +1,46 @@
<?php
namespace App\Listeners;
use App\Events\BusinessUserCreate;
use App\Models\Business;
use App\Models\User;
use App\Notifications\DBNotification;
use App\Notifications\MailNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Notification;
class BusinessUserCreateNotif
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param BusinessUserCreate $event
* @return void
*/
public function handle(BusinessUserCreate $event)
{
$payload = $event->message;
$new_user = User::findOrFail($payload->data->original->user_id);
$owners = Business::findOrFail($payload->business)->owners()->where('id', '!=', $new_user->id)->get();
$notif = [
'body' => __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name'), ['business' => request('_business_info')['name'], 'user' => $new_user->name])
];
$users = $owners->prepend($new_user);
Notification::send($users, new MailNotification($notif));
Notification::send($users, new DBNotification($notif));
}
}

32
app/Models/ReportableRelation.php

@ -4,6 +4,7 @@ namespace App\Models;
use Anik\Amqp\Exchange;
use Anik\Amqp\Facades\Amqp;
use App\Events\ModelSaved;
use PhpAmqpLib\Wire\AMQPTable;
use Anik\Amqp\PublishableMessage;
use Illuminate\Support\Facades\Auth;
@ -56,21 +57,22 @@ class ReportableRelation extends Pivot
'from' => env('CONTAINER_NAME'),
];
$message = new PublishableMessage(json_encode($payload));
$routers = [
"activity_exchange" => ["name" => "activity",],
"notif_exchange" => ["name" => "notif",],
"socket_exchange" => ["name" => "socket",],
];
foreach ($routers as $exchange => $properties) {
$message->setProperties(["application_headers" => new AMQPTable($properties)]);
$message->setExchange(new Exchange($exchange));
Amqp::publish($message, "");
}
ModelSaved::dispatch(json_encode($payload));
// $message = new PublishableMessage(json_encode($payload));
//
// $routers = [
// "activity_exchange" => ["name" => "activity",],
// "notif_exchange" => ["name" => "notif",],
// "socket_exchange" => ["name" => "socket",],
// ];
//
// foreach ($routers as $exchange => $properties) {
// $message->setProperties(["application_headers" => new AMQPTable($properties)]);
//
// $message->setExchange(new Exchange($exchange));
//
// Amqp::publish($message, "");
// }
}
public function properties()

10
app/Models/User.php

@ -44,6 +44,16 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
public $detach_relation = false;
/**
* Specifies the user's FCM token
*
* @return string
*/
public function routeNotificationForFcm()
{
return $this->fingerprints()->fcm_token;
}
public function updateRelations()
{
// projects relations

63
app/Notifications/DBNotification.php

@ -0,0 +1,63 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class DBNotification extends Notification
{
use Queueable;
public $message;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($message)
{
$this->message = $message;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'data' => $this->message['body']
];
}
}

48
app/Notifications/FcmNotification.php

@ -0,0 +1,48 @@
<?php
namespace App\Notifications;
use App\Channels\Messages\FcmMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class FcmNotification extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [FcmNotification::class];
}
/**
* Get the voice representation of the notification.
*
* @param mixed $notifiable
* @return FCMMessage
*/
public function toFcm($notifiable)
{
// return (new FcmMessage())
// ->to([])
}
}

2
app/Notifications/MailNotification.php

@ -67,7 +67,7 @@ class MailNotification extends Notification //implements ShouldQueue
public function toMail($notifiable)
{
return (new MailMessage)
->line(__('notification.'.$this->message))
->line($this->message['body'])
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}

9
app/Providers/AppServiceProvider.php

@ -2,6 +2,9 @@
namespace App\Providers;
use App\Channels\FcmChannel;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@ -13,7 +16,11 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{
//
Notification::resolved(function (ChannelManager $service) {
$service->extend('fcm', function ($app) {
return new FcmChannel(new HttpClient, config('fcm.key'));
});
});
}
/**

7
app/Providers/EventServiceProvider.php

@ -2,9 +2,11 @@
namespace App\Providers;
use App\Events\BusinessUserCreate;
use App\Events\ModelSaved;
use App\Events\TagCreate;
use App\Listeners\ActivityRegistration;
use App\Listeners\BusinessUserCreateNotif;
use App\Listeners\NotifHandler;
use App\Listeners\TagCreateNotif;
use Illuminate\Auth\Events\Registered;
@ -29,7 +31,10 @@ class EventServiceProvider extends ServiceProvider
],
TagCreate::class => [
TagCreateNotif::class,
]
],
BusinessUserCreate::class => [
BusinessUserCreateNotif::class,
],
];
/**

35
database/migrations/2021_03_08_114700_create_notifications_table.php

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateNotificationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notifications');
}
}

4
resources/lang/fa/notification.php

@ -15,6 +15,10 @@ return [
'tags' => [
'create' => 'یک تگ ساخته شد.'
],
'business_user' => [
'create' => 'کاربر :user به کسب و کار :business اضافه شد.'
]
];
Loading…
Cancel
Save