Merge branch 'staging' of https://dev.sismedika.online/febio/aso into staging

This commit is contained in:
2024-05-30 10:26:39 +07:00
10 changed files with 353 additions and 46 deletions

View File

@@ -48,8 +48,18 @@ class AuthController extends Controller
$user = User::with('detail') $user = User::with('detail')
->where('sEmail', $request->phone_or_email) ->where('sEmail', $request->phone_or_email)
->first(); ->first();
$user->fcm_token = $request->fcm_token;
$user->save(); $user->notificationTokens()->updateOrCreate([
'device_id' => $request->device_id,
'token' => $request->fcm_token,
], [
'origin' => $request->origin,
'device_id' => $request->device_id,
'type' => $request->type,
'token' => $request->fcm_token,
'status' => $request->status,
]);
return Helper::responseJson( return Helper::responseJson(
data: [ data: [
'token' => $user->createToken('app')->plainTextToken, 'token' => $user->createToken('app')->plainTextToken,

View File

@@ -25,7 +25,7 @@ class AuthDoctorController extends Controller
$data = [ $data = [
'email' => $request->email, 'email' => $request->email,
'password' => $request->password, 'password' => $request->password,
'fcm_token' => $request->fcm_token 'fcm_token' => $request->fcm_token,
]; ];
$validator = Validator::make($request->all(), [ $validator = Validator::make($request->all(), [
'email' => 'required|email', 'email' => 'required|email',
@@ -53,8 +53,17 @@ class AuthDoctorController extends Controller
return ApiResponse::apiResponse('Bad Request', $data, trans('Message.password'), 400); return ApiResponse::apiResponse('Bad Request', $data, trans('Message.password'), 400);
} }
$user->fcm_token = $request->fcm_token; $user->notificationTokens()->updateOrCreate([
$user->save(); 'device_id' => $request->device_id,
'token' => $request->fcm_token,
], [
'origin' => $request->origin,
'device_id' => $request->device_id,
'type' => $request->type,
'token' => $request->fcm_token,
'status' => $request->status,
]);
$res_data = [ $res_data = [
// 'user' => $user, // 'user' => $user,

View File

@@ -24,6 +24,8 @@ use Modules\HospitalPortal\Helpers\ApiResponse;
use App\Helpers\Helper; use App\Helpers\Helper;
use Illuminate\Support\Facades\View; use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Laravel\Firebase\Facades\Firebase;
class ChatDoctorController extends Controller class ChatDoctorController extends Controller
{ {
@@ -192,7 +194,6 @@ class ChatDoctorController extends Controller
]; ];
$user = UserLMS::where('nID',$livechat->patient_id)->first(); $user = UserLMS::where('nID',$livechat->patient_id)->first();
if ($user) { if ($user) {
$user->notify(new SendNotification($title, $body, $dataNotif)); $user->notify(new SendNotification($title, $body, $dataNotif));
return ApiResponse::apiResponse("Success",['message' => 'Livechat updated successfully'], trans('Message.success'), 200); return ApiResponse::apiResponse("Success",['message' => 'Livechat updated successfully'], trans('Message.success'), 200);

View File

@@ -23,6 +23,9 @@ use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\DuitkuController; use App\Http\Controllers\DuitkuController;
use DB; use DB;
use Illuminate\Contracts\Filesystem\Cloud;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Laravel\Firebase\Facades\Firebase;
use Str; use Str;
class LivechatController extends Controller class LivechatController extends Controller

View File

@@ -73,5 +73,8 @@ class User extends Authenticatable
return $this->morphMany(NotificationToken::class, 'notifiabletoken'); return $this->morphMany(NotificationToken::class, 'notifiabletoken');
} }
public function routeNotificationForFcm()
{
return $this->notificationTokens()->pluck('token')->toArray();
}
} }

View File

@@ -102,13 +102,18 @@ class User extends Authenticatable
return $this->hasMany(Person::class, 'owner_user_id'); return $this->hasMany(Person::class, 'owner_user_id');
} }
public function getOrganization()
{
return $this->hasOne(OrganizationUser::class, 'user_id');
}
public function notificationTokens() public function notificationTokens()
{ {
return $this->morphMany(NotificationToken::class, 'notifiabletoken'); return $this->morphMany(NotificationToken::class, 'notifiabletoken');
} }
public function getOrganization() public function routeNotificationForFcm()
{ {
return $this->hasOne(OrganizationUser::class, 'user_id'); return $this->notificationTokens()->pluck('token')->toArray();
} }
} }

View File

@@ -4,8 +4,17 @@ namespace App\Notifications;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Notification;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Laravel\Firebase\Facades\Firebase;
use NotificationChannels\Fcm\FcmChannel; use NotificationChannels\Fcm\FcmChannel;
use NotificationChannels\Fcm\FcmMessage; use NotificationChannels\Fcm\FcmMessage;;
use NotificationChannels\Fcm\Resources\AndroidConfig;
use NotificationChannels\Fcm\Resources\AndroidFcmOptions;
use NotificationChannels\Fcm\Resources\AndroidNotification;
use NotificationChannels\Fcm\Resources\ApnsConfig;
use NotificationChannels\Fcm\Resources\ApnsFcmOptions;
use NotificationChannels\Fcm\Resources\Notification as FcmNotification;
class SendNotification extends Notification class SendNotification extends Notification
{ {
@@ -19,7 +28,7 @@ class SendNotification extends Notification
{ {
$this->title = $title; $this->title = $title;
$this->body = $body; $this->body = $body;
$this->data = $data; $this->data = is_array($data) ? $data : (array) $data; // Pastikan data adalah array
} }
public function via($notifiable) public function via($notifiable)
@@ -27,13 +36,55 @@ class SendNotification extends Notification
return [FcmChannel::class]; return [FcmChannel::class];
} }
// public function toFcm($notifiable)
// {
// return FcmMessage::create()
// ->setData($this->data)
// ->setNotification([
// 'title' => $this->title,
// 'body' => $this->body,
// ]);
// }
public function toFcm($notifiable) public function toFcm($notifiable)
{ {
return FcmMessage::create() $deviceTokens = $notifiable->routeNotificationFor('fcm');
->setData($this->data) $notification = [
->setNotification([ 'title' => $this->title,
'title' => $this->title, 'body' => $this->body,
'body' => $this->body, ];
]);
if (count($deviceTokens)){
foreach($deviceTokens as $token) {
$message = CloudMessage::withTarget('token', $token)
->withNotification($notification) // optional
->withData($this->data);
Firebase::messaging()->send($message);
}
}
$dataFcm = FcmMessage::create()
->setToken($deviceTokens[0])
->setData([])
->setNotification(
FcmNotification::create()
->setTitle('ini title')
->setBody('ini body')
)
->setAndroid(
AndroidConfig::create()
->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
)->setApns(
ApnsConfig::create()
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios'))
);
return $dataFcm;
} }
public function fcmProject($notifiable, $message){
return 'app';
}
} }

192
config/firebase.php Normal file
View File

@@ -0,0 +1,192 @@
<?php
declare(strict_types=1);
return [
/*
* ------------------------------------------------------------------------
* Default Firebase project
* ------------------------------------------------------------------------
*/
'default' => env('FIREBASE_PROJECT', 'app'),
/*
* ------------------------------------------------------------------------
* Firebase project configurations
* ------------------------------------------------------------------------
*/
'projects' => [
'app' => [
/*
* ------------------------------------------------------------------------
* Credentials / Service Account
* ------------------------------------------------------------------------
*
* In order to access a Firebase project and its related services using a
* server SDK, requests must be authenticated. For server-to-server
* communication this is done with a Service Account.
*
* If you don't already have generated a Service Account, you can do so by
* following the instructions from the official documentation pages at
*
* https://firebase.google.com/docs/admin/setup#initialize_the_sdk
*
* Once you have downloaded the Service Account JSON file, you can use it
* to configure the package.
*
* If you don't provide credentials, the Firebase Admin SDK will try to
* auto-discover them
*
* - by checking the environment variable FIREBASE_CREDENTIALS
* - by checking the environment variable GOOGLE_APPLICATION_CREDENTIALS
* - by trying to find Google's well known file
* - by checking if the application is running on GCE/GCP
*
* If no credentials file can be found, an exception will be thrown the
* first time you try to access a component of the Firebase Admin SDK.
*
*/
'credentials' => [
'file' => env('FIREBASE_CREDENTIALS', env('GOOGLE_APPLICATION_CREDENTIALS')),
/*
* If you want to prevent the auto discovery of credentials, set the
* following parameter to false. If you disable it, you must
* provide a credentials file.
*/
'auto_discovery' => true,
],
/*
* ------------------------------------------------------------------------
* Firebase Auth Component
* ------------------------------------------------------------------------
*/
'auth' => [
'tenant_id' => env('FIREBASE_AUTH_TENANT_ID'),
],
/*
* ------------------------------------------------------------------------
* Firebase Realtime Database
* ------------------------------------------------------------------------
*/
'database' => [
/*
* In most of the cases the project ID defined in the credentials file
* determines the URL of your project's Realtime Database. If the
* connection to the Realtime Database fails, you can override
* its URL with the value you see at
*
* https://console.firebase.google.com/u/1/project/_/database
*
* Please make sure that you use a full URL like, for example,
* https://my-project-id.firebaseio.com
*/
'url' => env('FIREBASE_DATABASE_URL'),
/*
* As a best practice, a service should have access to only the resources it needs.
* To get more fine-grained control over the resources a Firebase app instance can access,
* use a unique identifier in your Security Rules to represent your service.
*
* https://firebase.google.com/docs/database/admin/start#authenticate-with-limited-privileges
*/
// 'auth_variable_override' => [
// 'uid' => 'my-service-worker'
// ],
],
'dynamic_links' => [
/*
* Dynamic links can be built with any URL prefix registered on
*
* https://console.firebase.google.com/u/1/project/_/durablelinks/links/
*
* You can define one of those domains as the default for new Dynamic
* Links created within your project.
*
* The value must be a valid domain, for example,
* https://example.page.link
*/
'default_domain' => env('FIREBASE_DYNAMIC_LINKS_DEFAULT_DOMAIN'),
],
/*
* ------------------------------------------------------------------------
* Firebase Cloud Storage
* ------------------------------------------------------------------------
*/
'storage' => [
/*
* Your project's default storage bucket usually uses the project ID
* as its name. If you have multiple storage buckets and want to
* use another one as the default for your application, you can
* override it here.
*/
'default_bucket' => env('FIREBASE_STORAGE_DEFAULT_BUCKET'),
],
/*
* ------------------------------------------------------------------------
* Caching
* ------------------------------------------------------------------------
*
* The Firebase Admin SDK can cache some data returned from the Firebase
* API, for example Google's public keys used to verify ID tokens.
*
*/
'cache_store' => env('FIREBASE_CACHE_STORE', 'file'),
/*
* ------------------------------------------------------------------------
* Logging
* ------------------------------------------------------------------------
*
* Enable logging of HTTP interaction for insights and/or debugging.
*
* Log channels are defined in config/logging.php
*
* Successful HTTP messages are logged with the log level 'info'.
* Failed HTTP messages are logged with the the log level 'notice'.
*
* Note: Using the same channel for simple and debug logs will result in
* two entries per request and response.
*/
'logging' => [
'http_log_channel' => env('FIREBASE_HTTP_LOG_CHANNEL'),
'http_debug_log_channel' => env('FIREBASE_HTTP_DEBUG_LOG_CHANNEL'),
],
/*
* ------------------------------------------------------------------------
* HTTP Client Options
* ------------------------------------------------------------------------
*
* Behavior of the HTTP Client performing the API requests
*/
'http_client_options' => [
/*
* Use a proxy that all API requests should be passed through.
* (default: none)
*/
'proxy' => env('FIREBASE_HTTP_CLIENT_PROXY'),
/*
* Set the maximum amount of seconds (float) that can pass before
* a request is considered timed out
*
* The default time out can be reviewed at
* https://github.com/kreait/firebase-php/blob/6.x/src/Firebase/Http/HttpClientOptions.php
*/
'timeout' => env('FIREBASE_HTTP_CLIENT_TIMEOUT'),
],
],
],
];

View File

@@ -17,6 +17,7 @@ return new class extends Migration
$table->id(); $table->id();
$table->morphs('notifiabletoken', 'notifiabletoken'); $table->morphs('notifiabletoken', 'notifiabletoken');
$table->string('origin'); $table->string('origin');
$table->string('device_id')->nullable();
$table->string('type'); $table->string('type');
$table->string('token'); $table->string('token');
$table->string('status'); $table->string('status');

View File

@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('notifications', function (Blueprint $table) {
$table->text('data')->after('notifiable_id')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('notifications', function (Blueprint $table) {
$table->dropColumn('data');
});
}
};