Skip to content

Commit

Permalink
add notification settings for FCM
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Oct 4, 2022
1 parent d203265 commit 7722f4e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 62 deletions.
11 changes: 0 additions & 11 deletions Database/Migrations/2022_09_16_031705_notifications_settings.php

This file was deleted.

18 changes: 18 additions & 0 deletions Database/Migrations/2022_09_18_031705_notifications_settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Spatie\LaravelSettings\Migrations\SettingsMigration;

class NotificationsSettings extends SettingsMigration
{
public function up(): void
{
$this->migrator->add('notifications.notifications_allow', false);
$this->migrator->add('notifications.fcm_apiKey', '');
$this->migrator->add('notifications.fcm_authDomain', '');
$this->migrator->add('notifications.fcm_projectId', '');
$this->migrator->add('notifications.fcm_storageBucket', '');
$this->migrator->add('notifications.fcm_messagingSenderId', '');
$this->migrator->add('notifications.fcm_appId', '');
$this->migrator->add('notifications.fcm_measurementId', '');
}
}
60 changes: 30 additions & 30 deletions Http/Controllers/NotificationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,36 +115,36 @@ class NotificationsController extends Resource
// return back();
// }
//
// public function token(Request $request)
// {
// $request->validate([
// "token" => "required|string",
// "provider" => "required|string",
// "model" => "required|string",
// "model_id" => "required"
// ]);
//
// $checkEx = UserToken::where('model_type', $request->get('model'))
// ->where('model_id', $request->get('model_id'))
// ->where('provider', $request->get('provider'))
// ->first();
//
// if (!$checkEx) {
// $token = new UserToken();
// $token->model_type = $request->get('model');
// $token->model_id = $request->get('model_id');
// $token->provider = $request->get('provider');
// $token->provider_token = $request->get('token');
// $token->save();
//
// return back();
// } else {
// $checkEx->provider_token = $request->get('token');
// $checkEx->save();
//
// return back();
// }
// }
public function token(Request $request)
{
$request->validate([
"token" => "required|string",
"provider" => "required|string",
"model" => "required|string",
"model_id" => "required"
]);

$checkEx = UserToken::where('model_type', $request->get('model'))
->where('model_id', $request->get('model_id'))
->where('provider', $request->get('provider'))
->first();

if (!$checkEx) {
$token = new UserToken();
$token->model_type = $request->get('model');
$token->model_id = $request->get('model_id');
$token->provider = $request->get('provider');
$token->provider_token = $request->get('token');
$token->save();

return back();
} else {
$checkEx->provider_token = $request->get('token');
$checkEx->save();

return back();
}
}
//
// public function routes(){
// return [];
Expand Down
4 changes: 2 additions & 2 deletions Notifications/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function toFcm($notifiable): FcmMessage
'type' => $this->type,
'privacy' => $this->privacy,
'model' => (string)$this->model,
'model_id' => (string)$this->model_id,
'model_id' => (string)$this->modelId,
])
->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
->setTitle($this->title)
Expand Down Expand Up @@ -162,7 +162,7 @@ public function toPushNotification($notifiable): PusherMessage
->setOption('type', $this->type)
->setOption('privacy', $this->privacy)
->setOption('model', $this->model)
->setOption('model_id', $this->model_id)
->setOption('model_id', $this->modelId)
->withAndroid(
PusherMessage::create()
->IOS()
Expand Down
14 changes: 11 additions & 3 deletions Pages/NotificationsSettingsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Modules\Notifications\Pages;

use Modules\Base\Services\Rows\Toggle;
use Modules\Notifications\Settings\NotificationsSettings;
use Modules\Base\Services\Rows\Text;
use Modules\Settings\Services\Setting;
Expand All @@ -12,13 +13,20 @@ class NotificationsSettingsPage extends Setting
public ?string $setting = NotificationsSettings::class;
public ?bool $api = true;
public ?string $path = "notifications";
public ?string $group = "Settings";
public ?string $icon = "bx bxs-circle";
public ?string $group = "Notifications";
public ?string $icon = "bx bxs-cog";

public function rows(): array
{
return [
Text::make('notifications_name')->label(__('Notifications Name')),
Toggle::make('notifications_allow')->label(__('Notifications Allow')),
Text::make('fcm_apiKey')->label(__('FCM apiKey')),
Text::make('fcm_authDomain')->label(__('FCM authDomain')),
Text::make('fcm_projectId')->label(__('FCM projectId')),
Text::make('fcm_storageBucket')->label(__('FCM storageBucket')),
Text::make('fcm_messagingSenderId')->label(__('FCM messagingSenderId')),
Text::make('fcm_appId')->label(__('FCM appId')),
Text::make('fcm_measurementId')->label(__('FCM measurementId')),
];
}
}
26 changes: 15 additions & 11 deletions Providers/NotificationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,26 @@ public function boot()
$this->loadMigrationsFrom(module_path($this->moduleName, 'Database/Migrations'));

VILT::loadResources($this->moduleName);
VILT::loadPages($this->moduleName);

$this->commands([
InstallNotifications::class
]);

$path = str_replace("storage/", "", setting('google_firebase_config'));
if($path){
if(File::exists(storage_path('app/public/' . $path))) {
$json = File::get(storage_path('app/public/' . $path));
VILT::registerShareData(Share::make('fcm')->data([
"config" => json_decode($json),
"vapidKey" => setting('google_firebase_vapid')
]));
}
}

VILT::registerShareData(Share::make('fcm')->data([
"config" => [
"apiKey"=> setting('fcm_apiKey'),
"authDomain"=> setting('fcm_authDomain'),
"projectId"=> setting('fcm_projectId'),
"storageBucket"=> setting('fcm_storageBucket'),
"messagingSenderId"=> setting('fcm_messagingSenderId'),
"appId"=> setting('fcm_appId'),
"measurementId"=> setting('fcm_measurementId'),
],
"vapidKey" => setting('google_firebase_vapid')
]));

VILT::registerTranslation(Lang::make('notifications.sidebar')->label(__('Notifications Settings')));
VILT::registerTranslation(Lang::make('notifiactions_templates.sidebar')->label(__('Notifications Templates')));
VILT::registerTranslation(Lang::make('user_notifications.sidebar')->label(__('Notifications')));
VILT::registerTranslation(Lang::make('notifications_logs.sidebar')->label(__('Notifications Logs')));
Expand Down
8 changes: 4 additions & 4 deletions Services/Actions/FireEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function fire(): void
/*
* Check if notification saved on database
*/
if ($this->database) {
if ($this->database || array_key_exists('database', $this->providers)) {
$this->sendToDatabase();
}
/*
Expand All @@ -69,7 +69,7 @@ public function fire(): void
/*
* Check if notification saved on database
*/
if ($this->database) {
if ($this->database || array_key_exists('database', $this->providers)) {
$this->sendToDatabase();
}

Expand Down Expand Up @@ -99,13 +99,13 @@ public function fire(): void
if (!empty($this->template)) {
$loadTemplate = $this->loadTemplate();
if ($loadTemplate) {
if ($this->database) {
if ($this->database || array_key_exists('database', $this->providers)) {
$this->sendToDatabase();
}
$this->sendToJob();
}
} else {
if ($this->database) {
if ($this->database || array_key_exists('database', $this->providers)) {
$this->sendToDatabase();
}
$this->sendToJob();
Expand Down
9 changes: 8 additions & 1 deletion Settings/NotificationsSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@

class NotificationsSettings extends Settings
{
public string $notifications_name = '';
public bool $notifications_allow = true;
public string $fcm_apiKey = '';
public string $fcm_authDomain = '';
public string $fcm_projectId = '';
public string $fcm_storageBucket = '';
public string $fcm_messagingSenderId = '';
public string $fcm_appId = '';
public string $fcm_measurementId = '';

public static function group(): string
{
Expand Down

0 comments on commit 7722f4e

Please sign in to comment.