3 Commits

Author SHA1 Message Date
mahdihty f13cdf4d46 some bug fix and change in notification and listener 4 years ago
mahdihty 5328ac2bbb add empty sms notif 4 years ago
mahdihty 69f7e7ee6a add business_id to DBNotification.php 4 years ago
  1. 45
      app/Channels/DBChannel.php
  2. 3
      app/Http/Controllers/NotificationController.php
  3. 5
      app/Listeners/BusinessUserCreateNotif.php
  4. 2
      app/Listeners/ProjectUserCreateNotif.php
  5. 15
      app/Notifications/DBNotification.php
  6. 1
      app/Notifications/FcmNotification.php
  7. 63
      app/Notifications/SmsNotification.php
  8. 4
      app/Providers/AppServiceProvider.php
  9. 3
      app/Utilities/HelperClass/NotificationHelper.php

45
app/Channels/DBChannel.php

@ -0,0 +1,45 @@
<?php
namespace App\Channels;
use Illuminate\Notifications\Channels\DatabaseChannel;
use Illuminate\Notifications\Notification;
class DBChannel extends DatabaseChannel
{
/**
* Get the data for the notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return array
*
* @throws \RuntimeException
*/
protected function getOptionalData($notifiable, Notification $notification)
{
if (method_exists($notification, 'toDatabaseOptional')) {
return is_array($data = $notification->toDatabaseOptional($notifiable))
? $data : [$data];
}
}
/**
* Build an array payload for the DatabaseNotification Model.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return array
*/
protected function buildPayload($notifiable, Notification $notification)
{
return [
'id' => $notification->id,
'type' => get_class($notification),
'data' => $this->getData($notifiable, $notification),
'read_at' => null,
] + $this->getOptionalData($notifiable, $notification);
}
}

3
app/Http/Controllers/NotificationController.php

@ -9,7 +9,8 @@ class NotificationController extends Controller
{
public function index($business)
{
return Auth::user()->notifications()->where('business_id', $business)->get();
return Auth::user()->notifications()
->where('data->business_id', $business)->get();
}
public function markAsRead($business, $notification)

5
app/Listeners/BusinessUserCreateNotif.php

@ -36,11 +36,12 @@ class BusinessUserCreateNotif
$payload = $event->message;
if ($payload->data->original->level === enum('levels.inactive.id')) {
// When user level in business is zero, probably user added to business by system
// And not necessary send notification to stockholders
// And not necessary send notification to stakeholders
return;
}
$new_user = User::findOrFail($payload->data->original->user_id);
$owners = Business::findOrFail($payload->business)->owners()->where('id', '!=', $new_user->id)->get();
$owners = Business::findOrFail($payload->business)->owners()
->whereNotIn('id', [$new_user->id, auth()->id()])->get();
$users = $owners->prepend($new_user);

2
app/Listeners/ProjectUserCreateNotif.php

@ -37,7 +37,7 @@ class ProjectUserCreateNotif
$new_user = User::findOrFail($payload->data->original->user_id);
$project = Project::findOrFail($payload->data->original->project_id);
$owners_id = request('_business_info')['info']['projects'][$project->id]['members']->reject(function ($item, $key) use ($new_user) {
return $item['level'] < enum('levels.owner.id') || $key === $new_user->id;
return $item['level'] < enum('levels.owner.id') || $key === $new_user->id || $key === auth()->id();
})->toArray();
$owners = User::whereIn('id', array_keys($owners_id))->get();

15
app/Notifications/DBNotification.php

@ -29,7 +29,7 @@ class DBNotification extends Notification
*/
public function via($notifiable)
{
return ['database'];
return ['db'];
}
/**
@ -42,7 +42,18 @@ class DBNotification extends Notification
{
return [
'data' => $this->message['body'],
'business_id' => 1
];
}
/**
* Get the array for optional database field that added to original table.
* @param $notifiable
* @return array
*/
public function toDatabaseOptional($notifiable)
{
return [
'business_id' => $this->message['business_id'] ?? null
];
}
}

1
app/Notifications/FcmNotification.php

@ -45,6 +45,7 @@ class FcmNotification extends Notification
->data([
'title' => $this->message['title'],
'body' => $this->message['body'],
'openURL' => "https://facebook.com/"
]);
}

63
app/Notifications/SmsNotification.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 SmsNotification 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 ['mail'];
}
/**
* 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 [
//
];
}
}

4
app/Providers/AppServiceProvider.php

@ -2,6 +2,7 @@
namespace App\Providers;
use App\Channels\DBChannel;
use App\Channels\FcmChannel;
use App\Channels\SocketChannel;
use Illuminate\Notifications\ChannelManager;
@ -25,6 +26,9 @@ class AppServiceProvider extends ServiceProvider
$service->extend('socket', function ($app) {
return new SocketChannel(new HttpClient, config('socket.url'));
});
$service->extend('db', function ($app) {
return new DBChannel();
});
});
}

3
app/Utilities/HelperClass/NotificationHelper.php

@ -27,7 +27,8 @@ class NotificationHelper
'greeting' => $this->getMessageLine($payload, $route.'greeting'),
'subject' => $this->getMessageLine($payload, $route.'subject'),
'title' => $this->getMessageLine($payload, $route.'title'),
'body' => $this->getMessageLine($payload, $route.'body', $options)
'body' => $this->getMessageLine($payload, $route.'body', $options),
'business_id' => request('_business_info')['id']
];
return $this;
}

Loading…
Cancel
Save