123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
-
- namespace App\Http\Controllers;
-
- use Illuminate\Http\Request;
- use App\Http\Controllers\Api\BaseController;
-
- use Config;
- use File;
- use Carbon\Carbon;
-
- use App\Model\Staff;
- use App\Model\StaffDetail;
- use App\Model\Module\Notification;
-
- class NotificationController extends BaseController
- {
- /**
- * Create save firebase token controller.
- *
- * @return json
- */
- public function saveToken(Request $request) {
-
- $staff = Staff::where('_id', $request->_id)->first();
- if(!empty($staff)){
- $staff->token_firebase = $request->token;
- $staff->save();
-
- return $this->sendResponse($staff,'Berjaya kemaskini firebase token');
- }else{
- return $this->sendError('','Tidak berjaya kemaskini firebase token');
- }
- }
-
- /**
- * Create notification controller.
- *
- * @return json
- */
-
- public function createNotifications($title, $msg, array $token) {
-
- $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
-
- $notification = [
- 'title' => $title,
- 'body' => $msg,
- 'icon' => '',
- 'sound' => true,
- ];
-
- $extraNotificationData = ["message" => $notification];
-
- $fcmNotification = [
- 'registration_ids' => $token, //multple token array
- 'notification' => $notification,
- 'data' => $extraNotificationData
- ];
-
- $headers = [
- 'Authorization: key=AAAAuCqc038:APA91bF0lSnEURlAscOjD6dRfPDaGnjH-jsePM6-AXcQTytN486RaM_zL8WoqDGTYnRZTeAh6nI8XFcQ9n293KJNfTKvdiNg_cOgnQN_4Yxqb0uK9hou8cmZuSMQgIw-QQl8Jl0WV51L',
- 'Content-Type: application/json'
- ];
-
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$fcmUrl);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
-
- $response = curl_exec($ch);
- curl_close($ch);
-
- return $response;
- }
-
- public function sendNotification(Request $request){
- $title = $request->title;
- $msg = $request->msg;
- $register_id = $request->register_id;
-
- if($register_id != null){
- $result = $this->createNotifications($title, $msg, $register_id);
- $response = json_decode($result);
-
- foreach ($response->results as $key => $r) {
- if(!empty($r->error)){}
- else if(!empty($r->message_id)){
- $staff = Staff::with('StaffDetail')->where('token_firebase',$register_id[$key])->first();
- if(!empty($staff)){
- $dataNoti = [
- 'title' => $request->title,
- 'body' => $request->msg,
- ];
- $staff->notification()->create($dataNoti);
- }
- }
- }
-
- return $this->sendResponse($response,'Berjaya hantar notifikasi');
- }
- }
-
- public function getNotificationMobile(Request $request){
- $staff = Staff::with('StaffDetail','Notification')->where('api_token', $request->api_token)->first();
- if(!empty($staff)){
- $noti = $staff->Notification;
- return $this->sendResponse($noti,'Berjaya dapatkan notifikasi');
- }else{
- return $this->sendError('','Kakitangan tidak ditemui!');
- }
- }
-
- public function getNotificationWeb(Request $request){
- $staff = Staff::with('StaffDetail','Notification')->where('_id', $request->_id)->first();
- if(!empty($staff)){
- $noti = $staff->Notification;
- return $this->sendResponse($noti,'Berjaya dapatkan notifikasi');
- }else{
- return $this->sendError('','Kakitangan tidak ditemui!');
- }
- }
-
- public function deleteNotification(Request $request){
- $noti = Notification::where('_id', $request->_id)->first();
- if(!empty($noti)){
- $noti->delete();
- return $this->sendResponse($noti,'Berjaya buang notifikasi');
- }else{
- return $this->sendError('','Data tidak ditemui!');
- }
- }
- }
|