load->helper(array('url')); $this->load->model('notification_model', 'notif'); date_default_timezone_set('Asia/Jakarta'); } /** * Simple health check for the notification API. */ public function index_get() { $this->response(array( 'message' => 'Notification API ready', ), 200); } /** * Generic FCM send endpoint. * * Expected JSON body: * { * "target": "device_or_topic", * "is_topic": false, * "data": { ... arbitrary key/value pairs ... }, * "title": "optional notification title", * "body": "optional notification body" * } */ public function send_generic_post() { $raw = file_get_contents('php://input'); $decoded = json_decode($raw, true); if (!is_array($decoded)) { $this->response(array( 'code' => '400', 'message' => 'invalid_json', ), 200); return; } $this->load->helper('fcm_v1_helper'); // Always verify FCM token is ready before using Firebase service (as in test / panel send). if (!fcm_v1_validate_token()) { $this->response(array( 'code' => '503', 'message' => 'fcm_token_not_ready', ), 200); return; } $target = isset($decoded['target']) ? trim($decoded['target']) : ''; $is_topic = !empty($decoded['is_topic']); $data = isset($decoded['data']) && is_array($decoded['data']) ? $decoded['data'] : array(); $title = isset($decoded['title']) ? (string) $decoded['title'] : ''; $body = isset($decoded['body']) ? (string) $decoded['body'] : ''; if ($target === '' || empty($data)) { $this->response(array( 'code' => '400', 'message' => 'missing_target_or_data', ), 200); return; } $options = array(); if ($title !== '' || $body !== '') { $options['title'] = $title; $options['body'] = $body; } if ($is_topic) { $result = $this->notif->send_generic_to_topic($target, $data, $options); } else { $result = $this->notif->send_generic_to_token($target, $data, $options); } if ($result === false) { $this->response(array( 'code' => '500', 'message' => 'fcm_send_failed_or_quota_exceeded', ), 200); return; } $this->response(array( 'code' => '200', 'message' => 'success', ), 200); } }