initial
This commit is contained in:
91
backendpanel/application/controllers/api/Notification.php
Normal file
91
backendpanel/application/controllers/api/Notification.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
require APPPATH . '/libraries/REST_Controller.php';
|
||||
|
||||
class Notification extends REST_Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->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;
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user