/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/PushNotificationController.php (2990B)
getRequest()->post();
if ($model->load($post) && $model->validate()) {
try {
$oneSignal = OneSignal::getInstance();
$response = $oneSignal->sendCustomerNotification(
['en' => Configuration::get(Configuration::APP_NAME)],
['en' => $model->message],
$model->app_type
);
// Log response for debugging
$responseData = json_decode($response, true);
if (isset($responseData['errors']) && !empty($responseData['errors'])) {
$errorMsg = is_array($responseData['errors']) ? implode(', ', $responseData['errors']) : $responseData['errors'];
Com::failureFlash('Push Notification Error: ' . $errorMsg);
Yii::error('OneSignal Error Response: ' . print_r($responseData, true), 'push-notification');
return $this->redirect(['/notification']);
} elseif (isset($responseData['id'])) {
Yii::info('Push notification sent successfully. ID: ' . $responseData['id'], 'push-notification');
} else {
Yii::warning('Unexpected OneSignal response: ' . print_r($responseData, true), 'push-notification');
}
} catch (\Exception $e) {
Com::failureFlash('Failed to send push notification: ' . $e->getMessage());
Yii::error('Push notification exception: ' . $e->getMessage() . ' | Trace: ' . $e->getTraceAsString(), 'push-notification');
return $this->redirect(['/notification']);
}
$modelNotify = new Notification();
$modelNotify->notification_title = $model->title;
$modelNotify->notification_description = utf8_encode($model->message);
$modelNotify->app_type = $model->app_type;
$modelNotify->status = Notification::ACTIVE;
$modelNotify->save(false);
// $model = Notification::find()->orderBy(['notification_id' => SORT_DESC])->all();
Com::successFlash('Push Notification sent successfully');
return $this->redirect(['/notification']);
}
return $this->render('create', [
'model' => new PushNotification(),
]);
}
}