Browse Source

Merge branch 'mahdi' of https://gitea.hooradev.ir/mahdihty/liwo into mohammad

pull/3/head
Mohammad Akbari 4 years ago
parent
commit
2476641563
Signed by: akbarjimi GPG Key ID: 55726AEFECE5E683
  1. 72
      app/Channels/FcmChannel.php
  2. 22
      app/Listeners/BusinessUserCreateNotif.php
  3. 3
      app/Models/Fingerprint.php
  4. 10
      app/Models/User.php
  5. 17
      app/Notifications/FcmNotification.php
  6. 5
      app/Notifications/MailNotification.php
  7. 1
      app/Providers/AppServiceProvider.php
  8. 2
      config/cors.php
  9. 18
      config/fcm.php
  10. 1
      database/migrations/2020_08_18_085017_fingerprints.php
  11. 14
      resources/lang/fa/notification.php

72
app/Channels/FcmChannel.php

@ -3,10 +3,46 @@
namespace App\Channels;
use App\Channels\Messages\FcmMessage;
use Illuminate\Notifications\Notification;
use GuzzleHttp\Client as HttpClient;
class FcmChannel
{
/**
* The API URL for FCM.
*
* @var string
*/
const API_URI = 'https://fcm.googleapis.com/fcm/send';
/**
* The HTTP client instance.
*
* @var \GuzzleHttp\Client
*/
protected $http;
/**
* The FCM API key.
*
* @var string
*/
protected $apikey;
/**
* Create a new FCM channel instance.
*
* @param \GuzzleHttp\Client $http
* @param string $apiKey
* @return void
*/
public function __construct(HttpClient $http, string $apiKey)
{
$this->http = $http;
$this->apiKey = $apiKey;
}
/**
* Send the given notification.
*
@ -18,6 +54,40 @@ class FcmChannel
{
$message = $notification->toFcm($notifiable);
$recipients = $notifiable->routeNotificationFor('fcm', $notification);
$message->to($notifiable->routeNotificationFor('fcm', $notification));
if (! $this->apiKey || (! $message->topic && ! $message->to)) {
return;
}
$this->http->post(self::API_URI, [
'headers' => [
'Authorization' => "key={$this->apiKey}",
'Content-Type' => 'application/json',
],
'json' => $this->buildJsonPayload($message),
]);
}
protected function buildJsonPayload(FcmMessage $message)
{
$payload = array_filter([
'priority' => $message->priority,
'data' => $message->data,
'notification' => $message->notification,
'condition' => $message->condition,
]);
if ($message->topic) {
$payload['to'] = "/topics/{$message->topic}";
} else {
if (is_array($message->to)) {
$payload['registration_ids'] = $message->to;
} else {
$payload['to'] = $message->to;
}
}
return $payload;
}
}

22
app/Listeners/BusinessUserCreateNotif.php

@ -2,10 +2,12 @@
namespace App\Listeners;
use App\Channels\FcmChannel;
use App\Events\BusinessUserCreate;
use App\Models\Business;
use App\Models\User;
use App\Notifications\DBNotification;
use App\Notifications\FcmNotification;
use App\Notifications\MailNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
@ -36,11 +38,29 @@ class BusinessUserCreateNotif
$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])
'greeting' => $this->getMessageLine($payload, 'greeting'),
'subject' => $this->getMessageLine($payload, 'subject'),
'title' => $this->getMessageLine($payload, 'title'),
'body' => $this->getMessageLine($payload, 'body', ['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));
Notification::send($users, new FcmNotification($notif));
}
/**
* Fetch message from notifications lang file
*
* @param $payload
* @param $key
* @param null $options
* @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
*
*/
public function getMessageLine($payload, $key, $options = null)
{
return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options);
}
}

3
app/Models/Fingerprint.php

@ -6,7 +6,7 @@ use App\Models\Model;
class Fingerprint extends Model
{
protected $fillable = ['user_id', 'agent', 'ip', 'os', 'latitude', 'longitude', 'token',];
protected $fillable = ['user_id', 'agent', 'ip', 'os', 'latitude', 'longitude', 'token', 'fcm_token'];
protected $table = 'fingerprints';
@ -25,6 +25,7 @@ class Fingerprint extends Model
'latitude' => 'required',
'longitude' => 'required',
'token' => 'required|string|min:60',
'fcm_token' => 'nullable',
];
}
}

10
app/Models/User.php

@ -43,6 +43,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->whereNotNull('fcm_token')->pluck('fcm_token')->all();
}
public function updateRelations()
{
// projects relations

17
app/Notifications/FcmNotification.php

@ -4,22 +4,22 @@ 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;
public $message;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
public function __construct($message)
{
//
$this->message = $message;
}
/**
@ -30,7 +30,7 @@ class FcmNotification extends Notification
*/
public function via($notifiable)
{
return [FcmNotification::class];
return ['fcm'];
}
/**
@ -41,8 +41,11 @@ class FcmNotification extends Notification
*/
public function toFcm($notifiable)
{
// return (new FcmMessage())
// ->to([])
return (new FcmMessage())
->data([
'title' => $this->message['title'],
'body' => $this->message['body'],
]);
}
}

5
app/Notifications/MailNotification.php

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

1
app/Providers/AppServiceProvider.php

@ -6,6 +6,7 @@ use App\Channels\FcmChannel;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\ServiceProvider;
use GuzzleHttp\Client as HttpClient;
class AppServiceProvider extends ServiceProvider
{

2
config/cors.php

@ -21,7 +21,7 @@ return [
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_origins_patterns' => ['Content-Type', 'X-Requested-With'],
'allowed_headers' => ['*'],

18
config/fcm.php

@ -0,0 +1,18 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| FCM API Key
|--------------------------------------------------------------------------
|
| This key allows you to send Push Notifications. To obtain this key go
| to the porject settings and click on the "Cloud Messaging" tab, now
| copy the API Key of "Legacy server key".
|
*/
'key' => env('FCM_KEY'),
];

1
database/migrations/2020_08_18_085017_fingerprints.php

@ -22,6 +22,7 @@ class Fingerprints extends Migration
$table->decimal('latitude', 10, 4);
$table->decimal('longitude', 11, 4);
$table->char('token', 60)->unique();
$table->text('fcm_token')->unique()->nullable();
$table->timestamps();
});
}

14
resources/lang/fa/notification.php

@ -18,12 +18,12 @@ return [
],
'business_user' => [
'create' => 'کاربر :user به کسب و کار :business اضافه شد.'
],
'business' => [
'update' => 'کاربر :user به کسب و کار :business اضافه شد.',
'update_wallet' => 'پول رو میدی یا پولت کنم',
],
'create' =>[
'greeting' => 'سلام کاربر گرامی!',
'subject' => 'افزودن کاربر به کسب و کار',
'title' => 'افزودن کاربر به کسب و کار',
'body' => 'کاربر :user به کسب و کار :business اضافه شد.'
]
]
];
Loading…
Cancel
Save