From 5e96eabedc6ef000453f826118af83ba41bd9044 Mon Sep 17 00:00:00 2001 From: ardhi Date: Fri, 25 Aug 2023 02:12:12 +0700 Subject: [PATCH 1/2] add broadcast API functionality (firebase) --- app/Http/Controllers/BroadcastController.php | 153 +++++++++++++++++++ app/Models/Broadcast.php | 2 +- app/Services/FCMService.php | 49 ++++++ bootstrap/app.php | 1 + routes/web.php | 23 ++- 5 files changed, 219 insertions(+), 9 deletions(-) create mode 100644 app/Services/FCMService.php diff --git a/app/Http/Controllers/BroadcastController.php b/app/Http/Controllers/BroadcastController.php index 7e0a8ca..5bb223e 100644 --- a/app/Http/Controllers/BroadcastController.php +++ b/app/Http/Controllers/BroadcastController.php @@ -3,7 +3,160 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use App\Models\Broadcast; +use App\Models\User; +use App\Services\FCMService; class BroadcastController extends Controller { + public function add(Request $request) + { + + $data = $request->all(); + $data['status_send'] = true; + $data['created_by'] = $this->currentName; + // dd($data); + $result = Broadcast::create($data); + if($result){ + $this->sendNotification($data); + return response()->json(['status'=>'success','message'=>'add broadcast successfully!','code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'add broadcast failed!','code'=>400], 400); + } + } + + public function edit($id){ + if(!$id || (int) $id < 0 || $id==""){ + return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); + die(); + } + + $result = Broadcast::find($id); + + if($result){ + return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'failed get data broadcast, please try again later!','code'=>400], 400); + } + } + + public function update(Request $request, $id) + { + if(!$id || (int) $id < 0 || $id==""){ + return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); + } + + $data = Broadcast::find($id); + + if($data){ + $result = $data->update($request->all()); + }else{ + return response()->json(['status'=>'failed','message'=>'data broadcast not found!','code'=>400], 400); + die(); + } + + + if($result){ + return response()->json(['status'=>'success','message'=>'data broadcast successfully updated!','code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'data broadcast failed updated!','code'=>400], 400); + } + } + + public function delete($id) + { + $data = Broadcast::find($id); + + if($data){ + $delete = $data->delete(); + }else{ + return response()->json(['status'=>'failed','message'=>'data broadcast not found!','code'=>400], 400); + die(); + } + + + if($delete){ + return response()->json(['status'=>'success','message'=>'data broadcast successfully deleted!','code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'data broadcast failed deleted!','code'=>400], 400); + } + } + + public function search(Request $request) + { + $payload = $request->all(); + $dataBuilder = $this->setUpPayload($payload, 'm_broadcast'); + $builder = $dataBuilder['builder']; + $countBuilder = $dataBuilder['count']; + $dataGet = $builder->get(); + $totalRecord = $countBuilder->count(); + return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); + } + + public function list() + { + $data = Broadcast::all(); + $countData = $data->count(); + + if($data){ + return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'failed get list broadcast, please try again later!','code'=>400], 400); + } + } + + public function sendNotification($data) + { + // send_to_type (all, roles, user) + if (isset($data['send_to_type'])) { + switch ($data['send_to_type']) { + case 'all': + $users = User::whereNotNull('fcm_token')->get(); + if (isset($users)) { + foreach ($users as $user) { + FCMService::send( + $user->fcm_token, + [ + 'title' => $data['title_notif'], + 'body' => $data['message_notif'], + ] + ); + } + } + break; + + case 'roles': + $users = User::where("role_id", $data['send_to_id'])->whereNotNull('fcm_token')->get(); + if (isset($users)) { + foreach ($users as $user) { + FCMService::send( + $user->fcm_token, + [ + 'title' => $data['title_notif'], + 'body' => $data['message_notif'], + ] + ); + } + } + break; + + case 'user': + $user = User::where("id", $data['send_to_id'])->whereNotNull('fcm_token')->first(); + if (isset($user)) { + FCMService::send( + $user->fcm_token, + [ + 'title' => $data['title_notif'], + 'body' => $data['message_notif'], + ] + ); + } + break; + + default: + # code... + break; + } + } + } } diff --git a/app/Models/Broadcast.php b/app/Models/Broadcast.php index c2767c9..bacbdcd 100644 --- a/app/Models/Broadcast.php +++ b/app/Models/Broadcast.php @@ -12,6 +12,6 @@ class Broadcast extends Model const UPDATED_AT = 'updated_at'; protected $fillable = [ - 'title_notif', 'message_notif', 'description', 'send_to_type', 'created_at', 'created_by', 'updated_at', 'updated_by' + 'title_notif', 'message_notif', 'description', 'send_to_type', 'send_to_id', 'status_send', 'created_at', 'created_by', 'updated_at', 'updated_by' ]; } diff --git a/app/Services/FCMService.php b/app/Services/FCMService.php new file mode 100644 index 0000000..3fdf971 --- /dev/null +++ b/app/Services/FCMService.php @@ -0,0 +1,49 @@ + [$fcm_token], + "notification" => [ + "title" => $notification['title'], + "body" => $notification['body'], + ] + ]; + $encodedData = json_encode($data); + + $headers = [ + 'Authorization:key=' . $serverKey, + 'Content-Type: application/json', + ]; + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); + // Disabling SSL Certificate support temporarly + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedData); + + $result = curl_exec($ch); + if ($result === FALSE) { + return array("success"=> false, "message"=> curl_error($ch)); + } + // Close connection + curl_close($ch); + return array("success"=> true, "message"=> $result); + } +} \ No newline at end of file diff --git a/bootstrap/app.php b/bootstrap/app.php index af68fb8..13cdeb8 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -63,6 +63,7 @@ $app->configure('auth'); $app->configure('api'); $app->configure('assets'); $app->configure('app'); +$app->configure('fcm'); /* |-------------------------------------------------------------------------- diff --git a/routes/web.php b/routes/web.php index 26b1365..2b97a41 100644 --- a/routes/web.php +++ b/routes/web.php @@ -447,16 +447,23 @@ $router->group(['prefix'=>'api', 'middleware' => 'cors'], function () use ($rout $router->put('/project-comment/update/{id}', 'ProjectCommentController@update'); $router->post('/project-comment/search', 'ProjectCommentController@search'); - $router->get('/hierarchy-ftths', 'HierarchyFtthController@index'); - $router->post('/hierarchy-ftths', 'HierarchyFtthController@store'); - $router->post('/hierarchy-ftths/search', 'HierarchyFtthController@search'); - $router->get('/hierarchy-ftths/{id}', 'HierarchyFtthController@show'); - $router->put('/hierarchy-ftths/{id}', 'HierarchyFtthController@update'); - $router->delete('/hierarchy-ftths/{id}', 'HierarchyFtthController@destroy'); - $router->get('/hierarchy-ftths/tree/{project_id}', 'HierarchyFtthController@getTreeByProject'); - $router->get('/hierarchy-ftths/tree-gantt/{gantt_id}', 'HierarchyFtthController@getTreeByGantt'); + $router->get('/hierarchy-ftths', 'HierarchyFtthController@index'); + $router->post('/hierarchy-ftths', 'HierarchyFtthController@store'); + $router->post('/hierarchy-ftths/search', 'HierarchyFtthController@search'); + $router->get('/hierarchy-ftths/{id}', 'HierarchyFtthController@show'); + $router->put('/hierarchy-ftths/{id}', 'HierarchyFtthController@update'); + $router->delete('/hierarchy-ftths/{id}', 'HierarchyFtthController@destroy'); + $router->get('/hierarchy-ftths/tree/{project_id}', 'HierarchyFtthController@getTreeByProject'); + $router->get('/hierarchy-ftths/tree-gantt/{gantt_id}', 'HierarchyFtthController@getTreeByGantt'); $router->post('/map-monitoring/search', 'MapMonitoringController@search'); + + $router->post('/broadcast/add', 'BroadcastController@add'); + $router->get('/broadcast/edit/{id}', 'BroadcastController@edit'); + $router->put('/broadcast/update/{id}', 'BroadcastController@update'); + $router->post('/broadcast/search', 'BroadcastController@search'); + $router->delete('/broadcast/delete/{id}', 'BroadcastController@delete'); + $router->get('/broadcast/list', 'BroadcastController@list'); }); }); From 3188eba18dbc017f700a4db1aa7ae0512b049e9f Mon Sep 17 00:00:00 2001 From: ardhi Date: Fri, 25 Aug 2023 02:12:26 +0700 Subject: [PATCH 2/2] add broadcast API functionality (firebase) --- config/fcm.php | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 config/fcm.php diff --git a/config/fcm.php b/config/fcm.php new file mode 100644 index 0000000..5c60ec9 --- /dev/null +++ b/config/fcm.php @@ -0,0 +1,5 @@ + "AAAAin2zZmc:APA91bHFIYDzZGyVyXvt2C8I09wC2k8siWPQIo4b1Db0QjxCzQR5SRQU9KY1iNRIUhTL6OoLUs2x6UAiP1BNv-mwOlSR7C_405msoNL2p33JVBxrtqc7hdMc5TEdTBB4ZGRVH7ltQzSe", +]; \ No newline at end of file