_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, $data, array $token) { $fcmUrl = 'https://fcm.googleapis.com/fcm/send'; $notification = [ 'title' => $title, 'body' => $msg, 'icon' => '', 'sound' => true, 'data' => $data, ]; $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; $data = $request->data; $register_id = $request->register_id; // info($data); if($register_id != null){ $result = $this->createNotifications($title, $msg, $data, $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' => $title, 'body' => $msg, 'data' => $data, ]; $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!'); } } }