registry = $registry; } private $fcmUrl = "https://fcm.googleapis.com/fcm/send"; public function sendPushNotificationByEmpId($empId, $title, $message, $screen = '/dashboard') { $sqlCheck = "SELECT id, token FROM fcm_token WHERE emp_id = :emp_id"; $stmtCheck = $this->registry->db->prepare($sqlCheck); $stmtCheck->bindValue(':emp_id', $empId, PDO::PARAM_INT); $stmtCheck->execute(); $rsCheck = $stmtCheck->fetchAll(PDO::FETCH_ASSOC); $ctr = count($rsCheck); if ($ctr === 0) { return []; } $response = []; $toTokens = []; for ($i = 0; $i < $ctr; $i++) { array_push($toTokens, $rsCheck[$i]['token']); } $stmtDelete = $this->registry->db->prepare('delete from fcm_token where id = :id'); $temp = $this->sendPushNotification($toTokens, $title, $message, $empId, $screen); $dcode = json_decode($temp, true); for ($i = 0; $i < count($dcode['results']); $i++) { $status = [ 'is_send' => true, 'is_deleted' => false, 'fcm_token_id' => $rsCheck[$i]['id'], 'emp_id' => $empId ]; if (isset($dcode['results'][$i]['error']) && $dcode['results'][$i]['error'] === 'NotRegistered') { $stmtDelete->bindValue(':id', $rsCheck[$i]['id'], PDO::PARAM_INT); $stmtDelete->execute(); $status['is_send'] = false; $status['is_deleted'] = true; } array_push($response, $status); } return $response; } public function sendPushNotification($toToken = [], $title, $message, $userId = null, $screen = '/dashboard') { $header = [ 'authorization: key=' . $this->registry->config->fcm_token, 'content-type: application/json', ]; $notification = [ 'title' => $title, 'body' => $message, "click_action" => 'FLUTTER_NOTIFICATION_CLICK', ]; $extraNotificationData = [ 'screen' => $screen, "message" => $notification, "id" => $userId, ]; $fcmNotification = [ 'registration_ids' => $toToken, 'notification' => $notification, 'data' => $extraNotificationData, ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->fcmUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification)); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $result = curl_exec($ch); curl_close($ch); return $result; } }