From 90b1c635ef0efb2b7c86b4d068071209790026bd Mon Sep 17 00:00:00 2001 From: mahdihty Date: Tue, 16 Mar 2021 14:11:25 +0330 Subject: [PATCH] add sms make notif func to helper and some change --- app/Models/User.php | 10 ++++++++++ app/Providers/AppServiceProvider.php | 4 ++++ app/Utilities/HelperClass/NotificationHelper.php | 11 ++++++++++- resources/lang/fa/notification.php | 12 ++++++++++++ routes/api.php | 4 +++- 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index ea04af2..aa0357c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -60,6 +60,16 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac return request('_business_info')['id'] ?? null; } + /** + * Specifies the user's phone number + * + * @return string + */ + public function routeNotificationForSms() + { + return $this->mobile; + } + public function updateRelations() { // projects relations diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index d25685e..77c57a4 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -4,6 +4,7 @@ namespace App\Providers; use App\Channels\DBChannel; use App\Channels\FcmChannel; +use App\Channels\SmsChannel; use App\Channels\SocketChannel; use Illuminate\Notifications\ChannelManager; use Illuminate\Support\Facades\Notification; @@ -29,6 +30,9 @@ class AppServiceProvider extends ServiceProvider $service->extend('db', function ($app) { return new DBChannel(); }); + $service->extend('sms', function ($app) { + return new SmsChannel(new HttpClient, config('smsirlaravel.webservice-url')); + }); }); } diff --git a/app/Utilities/HelperClass/NotificationHelper.php b/app/Utilities/HelperClass/NotificationHelper.php index d128e9c..2d8859f 100644 --- a/app/Utilities/HelperClass/NotificationHelper.php +++ b/app/Utilities/HelperClass/NotificationHelper.php @@ -8,11 +8,14 @@ use App\Notifications\DBNotification; use App\Notifications\FcmNotification; use App\Notifications\MailNotification; use App\Notifications\SocketNotification; +use App\Notifications\SmsNotification; use Illuminate\Support\Facades\Notification; class NotificationHelper { protected $notif; + + protected $sms_notif; /** * Make notification object * @@ -33,6 +36,12 @@ class NotificationHelper return $this; } + public function makeSmsNotif($template_name, $options = []) + { + $this->sms_notif = __('notification.sms.templates.'.$template_name, $options); + return $this; + } + /** * Fetch message from notifications lang file * @@ -56,7 +65,7 @@ class NotificationHelper public function sendNotifications($users, $level = null) { switch ($level) { case "emergency": -// Notification::send($users, new SmsNotification($notif)); + Notification::send($users, new SmsNotification($this->sms_notif, 'ultraFastSend')); case "critical": Notification::send($users, new MailNotification($this->notif)); case "high": diff --git a/resources/lang/fa/notification.php b/resources/lang/fa/notification.php index 856ea0d..39d08d2 100644 --- a/resources/lang/fa/notification.php +++ b/resources/lang/fa/notification.php @@ -85,4 +85,16 @@ return [ 'suspended' => 'حساب مسدود شد.', ], + 'sms' => [ + 'templates' => [ + 'template_name' => [ + 'template_id' => 'asdasd', + 'params' => [ + 'user' => ':user', + 'business' => ':business', + ] + ] + ] + ] + ]; diff --git a/routes/api.php b/routes/api.php index adb8f8f..1f7b178 100644 --- a/routes/api.php +++ b/routes/api.php @@ -8,7 +8,9 @@ $router->get('/lab', function () { $router->get('/ntest', function () { $user = \App\Models\User::find(1); - \Illuminate\Support\Facades\Notification::send($user, new \App\Notifications\SocketNotification(['title' => "hello!!!", 'body' => 'sss'])); + \Illuminate\Support\Facades\Notification::send($user, new \App\Notifications\SmsNotification(['verification_code' => "1234"])); + (new \App\Utilities\HelperClass\NotificationHelper()) + ->makeSmsNotif('template_name', ['user' => 'myUser', 'business' => 'myBusiness']); })->middleware('bindBusiness'); $router->group(['prefix' => 'actions'], function () use ($router) { $router->group(['prefix' => 'businesses'], function () use ($router) {