97 lines
3.6 KiB
PHP
Executable File
97 lines
3.6 KiB
PHP
Executable File
<?php
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
class appnotification extends CI_Controller
|
|
{
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
is_logged_in();
|
|
|
|
$this->load->model('notification_model', 'notif');
|
|
$this->load->model('Pelanggan_model', 'pelanggan_model');
|
|
$this->load->library('form_validation');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$data['pelanggan_list'] = $this->pelanggan_model->get_all_for_notification_picker();
|
|
$this->load->view('includes/header');
|
|
$this->load->view('appnotification/index', $data);
|
|
$this->load->view('includes/footer');
|
|
}
|
|
|
|
public function send()
|
|
{
|
|
$this->load->helper('fcm_v1_helper');
|
|
|
|
// Validate Firebase token before kirim. If no token, relogin (retry) is done in helper.
|
|
if (!fcm_v1_validate_token()) {
|
|
$this->session->set_flashdata('error', 'Firebase token tidak valid. Pastikan file ngojol-trial-firebase-adminsdk JSON ada dan dapat dibaca. Silakan coba lagi.');
|
|
redirect('appnotification/index');
|
|
return;
|
|
}
|
|
|
|
$title = $this->input->post('title');
|
|
$message = $this->input->post('message');
|
|
$send_target = $this->input->post('send_target');
|
|
|
|
if ($send_target === 'users') {
|
|
$user_ids = $this->input->post('user_ids');
|
|
if (!is_array($user_ids)) {
|
|
$user_ids = array();
|
|
}
|
|
$user_ids = array_unique(array_filter(array_map('intval', $user_ids)));
|
|
if (empty($user_ids)) {
|
|
$this->session->set_flashdata('error', 'Pilih minimal satu pengguna (user).');
|
|
redirect('appnotification/index');
|
|
return;
|
|
}
|
|
$sent = 0;
|
|
$skipped = 0;
|
|
foreach ($user_ids as $uid) {
|
|
if ($uid <= 0) {
|
|
continue;
|
|
}
|
|
$row = $this->pelanggan_model->get_notification_row_by_id($uid);
|
|
if (!$row) {
|
|
$skipped++;
|
|
continue;
|
|
}
|
|
$tok = isset($row->token) ? trim((string) $row->token) : '';
|
|
if ($tok === '' || !fcm_v1_is_valid_device_token($tok)) {
|
|
$skipped++;
|
|
continue;
|
|
}
|
|
if ($this->notif->send_notif($title, $message, $tok)) {
|
|
$sent++;
|
|
} else {
|
|
$skipped++;
|
|
}
|
|
}
|
|
if ($sent > 0) {
|
|
$msg = 'Notifikasi terkirim ke ' . $sent . ' pengguna.';
|
|
if ($skipped > 0) {
|
|
$msg .= ' ' . $skipped . ' dilewati (tanpa token FCM valid atau gagal kirim).';
|
|
}
|
|
$this->session->set_flashdata('send', $msg);
|
|
} else {
|
|
$this->session->set_flashdata('error', 'Tidak ada notifikasi terkirim. Pastikan pengguna memiliki token FCM valid (login dari aplikasi).');
|
|
}
|
|
redirect('appnotification/index');
|
|
return;
|
|
}
|
|
|
|
$topic = $this->input->post('topic');
|
|
$ok = $this->notif->send_notif($title, $message, $topic);
|
|
if ($ok) {
|
|
$this->session->set_flashdata('send', 'Notifikasi berhasil dikirim');
|
|
} else {
|
|
$this->session->set_flashdata('error', 'Gagal mengirim notifikasi. Firebase token mungkin kedaluwarsa. Silakan coba lagi.');
|
|
}
|
|
redirect('appnotification/index');
|
|
}
|
|
}
|