Browse Source

complete Fcm push notification

channel, message, notification
add fcm_token to Fingerprint model
pull/1/head
mahdihty 4 years ago
parent
commit
0ea97f895b
  1. 72
      app/Channels/FcmChannel.php
  2. 8
      app/Listeners/BusinessUserCreateNotif.php
  3. 3
      app/Models/Fingerprint.php
  4. 10
      app/Models/User.php
  5. 17
      app/Notifications/FcmNotification.php
  6. 1
      app/Providers/AppServiceProvider.php
  7. 1
      database/migrations/2020_08_18_085017_fingerprints.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;
}
}

8
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,15 @@ 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' => __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.greeting'),
'subject' => __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.subject'),
'title' => __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.title'),
'body' => __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.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));
}
}

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'],
]);
}
}

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
{

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();
});
}

Loading…
Cancel
Save