mahdihty
4 years ago
14 changed files with 434 additions and 21 deletions
-
23app/Channels/FcmChannel.php
-
132app/Channels/Messages/FcmMessage.php
-
6app/Enums/cruds.php
-
38app/Events/BusinessUserCreate.php
-
46app/Listeners/BusinessUserCreateNotif.php
-
32app/Models/ReportableRelation.php
-
10app/Models/User.php
-
63app/Notifications/DBNotification.php
-
48app/Notifications/FcmNotification.php
-
2app/Notifications/MailNotification.php
-
9app/Providers/AppServiceProvider.php
-
7app/Providers/EventServiceProvider.php
-
35database/migrations/2021_03_08_114700_create_notifications_table.php
-
4resources/lang/fa/notification.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); |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
@ -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'); |
|||
} |
|||
} |
@ -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)); |
|||
} |
|||
} |
@ -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'] |
|||
]; |
|||
} |
|||
} |
@ -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([])
|
|||
} |
|||
|
|||
} |
@ -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'); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue