Browse Source

Merge pull request 'add broadcast api functionality (firebase)' (#156) from dev-ardhi into staging

Reviewed-on: ordo/adw-backend#156
pull/3/head
ibnu 1 year ago
parent
commit
2a397d293d
  1. 153
      app/Http/Controllers/BroadcastController.php
  2. 2
      app/Models/Broadcast.php
  3. 49
      app/Services/FCMService.php
  4. 1
      bootstrap/app.php
  5. 5
      config/fcm.php
  6. 23
      routes/web.php

153
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;
}
}
}
}

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

49
app/Services/FCMService.php

@ -0,0 +1,49 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class FCMService
{
public static function send($fcm_token, $notification)
{
$url = 'https://fcm.googleapis.com/fcm/send';
// $url = 'https://fcm.googleapis.com/v1/projects/594814396007/messages:send';
$serverKey = config('fcm.server_key');
$data = [
"registration_ids" => [$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);
}
}

1
bootstrap/app.php

@ -63,6 +63,7 @@ $app->configure('auth');
$app->configure('api');
$app->configure('assets');
$app->configure('app');
$app->configure('fcm');
/*
|--------------------------------------------------------------------------

5
config/fcm.php

@ -0,0 +1,5 @@
<?php
return [
'server_key' => "AAAAin2zZmc:APA91bHFIYDzZGyVyXvt2C8I09wC2k8siWPQIo4b1Db0QjxCzQR5SRQU9KY1iNRIUhTL6OoLUs2x6UAiP1BNv-mwOlSR7C_405msoNL2p33JVBxrtqc7hdMc5TEdTBB4ZGRVH7ltQzSe",
];

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

Loading…
Cancel
Save