File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/PushNotificationController.php
<?php
namespace App\Http\Controllers;
use \OneSignal;
use Berkayk\OneSignal\OneSignalClient;
define(
'DEFAULT_BUTTONS',
[
[
"id" => "Show",
"text" => "Ver notificaciones",
"ios_category" => "accept"
]
]
);
class PushNotificationController extends Controller
{
private $onesignalDelivery;
private function getOneSignalDelivery()
{
if ($this->onesignalDelivery) {
return $this->onesignalDelivery;
}
$this->onesignalDelivery = new OneSignalClient(config('onesignal.app_id_delivery'), config('onesignal.rest_api_key_delivery'), null);
return $this->onesignalDelivery;
}
/**
* * Envio de notificacion con destino absolutamente todos los usuarios registrados en el app de OneSignal
* @param $message - Mensaje a ser enviado
* @param $url - URL que redirecciona la notificacion
* @param $data - Datos en segundo plano enviados junto a la notificacion
* @param $buttons - Botones incluidos en la notificacion aplica con ciertas restricciones según version iOS y Android
* @param $schedule - Fecha y hora de programación de la notificacion formatos posibles:
* "Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)"
* "September 24th 2015, 2:00:00 pm UTC-07:00"
* "2015-09-24 14:00:00 GMT-0700"
* "Sept 24 2015 14:00:00 GMT-0700"
* "Thu Sep 24 2015 14:00:00 GMT-0700 (Pacific Daylight Time)"
* @param $typeTarget - U : USER D : DELIVERY
*/
public function sendToAll($message, $url, $data, $buttons, $schedule, $typeTarget, $headings = null, $subtitle = null)
{
if ($typeTarget == "D") {
$onesignalDelivery = $this->getOneSignalDelivery();
return $onesignalDelivery->sendNotificationToAll($message, $url, $data, $buttons ?? DEFAULT_BUTTONS, $schedule, $headings, $subtitle);
} else {
return OneSignal::sendNotificationToAll($message, $url, $data, $buttons ?? DEFAULT_BUTTONS, $schedule, $headings, $subtitle);
}
}
/**
* * Envio de notificacion con destino a un usuario según su Player ID.
* @param $message - Mensaje a ser enviado
* @param $idOneSignal - Player ID asignado por la palataforma de OneSignal.
* @param $url - URL que redirecciona la notificacion
* @param $data - Datos en segundo plano enviados junto a la notificacion
* @param $buttons - Botones incluidos en la notificacion aplica con ciertas restricciones según version iOS y Android
* @param $schedule - Fecha y hora de programación de la notificacion formatos posibles:
*
* "Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)"
* "September 24th 2015, 2:00:00 pm UTC-07:00"
* "2015-09-24 14:00:00 GMT-0700"
* "Sept 24 2015 14:00:00 GMT-0700"
* "Thu Sep 24 2015 14:00:00 GMT-0700 (Pacific Daylight Time)"
* @param $typeTarget - U : USER, D : DELIVERY
*/
public function sendToUser($message, $idOneSignal, $url, $data, $buttons, $schedule, $typeTarget, $headings = null, $subtitle = null)
{
if ($idOneSignal) {
if ($typeTarget == "D") {
$onesignalDelivery = $this->getOneSignalDelivery();
return $onesignalDelivery->sendNotificationToUser($message, $idOneSignal, $url, $data, $buttons ?? DEFAULT_BUTTONS, $schedule, $headings, $subtitle);
} else {
return OneSignal::sendNotificationToUser($message, $idOneSignal, $url, $data, $buttons ?? DEFAULT_BUTTONS, $schedule, $headings, $subtitle);
}
}
}
/**
* * Envio de notificacion con destino un segmento, el cual debe existir en la plataforma de OneSignal.
* @param $message - Mensaje a ser enviado
* @param $segment - Segmento, el cual debe estar creado en la plataforma de OneSignal.
* @param $url - URL que redirecciona la notificacion
* @param $data - Datos en segundo plano enviados junto a la notificacion
* @param $buttons - Botones incluidos en la notificacion aplica con ciertas restricciones según version iOS y Android
* @param $schedule - Fecha y hora de programación de la notificacion formatos posibles:
*
* "Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)"
* "September 24th 2015, 2:00:00 pm UTC-07:00"
* "2015-09-24 14:00:00 GMT-0700"
* "Sept 24 2015 14:00:00 GMT-0700"
* "Thu Sep 24 2015 14:00:00 GMT-0700 (Pacific Daylight Time)"
* @param $typeTarget - U : USER D : DELIVERY
*/
public function sendToSegments($message, $segment, $url, $data, $buttons, $schedule, $typeTarget, $headings = null, $subtitle = null)
{
if ($typeTarget == "D") {
$onesignalDelivery = $this->getOneSignalDelivery();
$onesignalDelivery->sendNotificationToSegment($message, $segment, $url, $data, $buttons ?? DEFAULT_BUTTONS, $schedule, $headings, $subtitle);
} else {
OneSignal::sendNotificationToSegment($message, $segment, $url, $data, $buttons ?? DEFAULT_BUTTONS, $schedule, $headings, $subtitle);
}
}
/**
* * Envio de notificacion con destino a las tags indicadas, las cuales deben existir en la plataforma de OneSignal.
* @param $message - Mensaje a ser enviado
* @param $tags - TAGS, etiquetas que deben estar marcadas en el usuario.
* @param $url - URL que redirecciona la notificacion
* @param $data - Datos en segundo plano enviados junto a la notificacion
* @param $buttons - Botones incluidos en la notificacion aplica con ciertas restricciones según version iOS y Android
* @param $schedule - Fecha y hora de programación de la notificacion formatos posibles:
*
* "Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)"
* "September 24th 2015, 2:00:00 pm UTC-07:00"
* "2015-09-24 14:00:00 GMT-0700"
* "Sept 24 2015 14:00:00 GMT-0700"
* "Thu Sep 24 2015 14:00:00 GMT-0700 (Pacific Daylight Time)"
* @param $typeTarget - U : USER D : DELIVERY
*/
public function sendToSegment($message, $tags, $url, $data, $buttons, $schedule, $typeTarget, $headings = null, $subtitle = null)
{
if ($typeTarget == "D") {
$onesignalDelivery = $this->getOneSignalDelivery();
$onesignalDelivery->sendNotificationUsingTags($message, $tags, $url, $data, $buttons ?? DEFAULT_BUTTONS, $schedule, $headings, $subtitle);
} else {
OneSignal::sendNotificationUsingTags($message, $tags, $url, $data, $buttons ?? DEFAULT_BUTTONS, $schedule, $headings, $subtitle);
}
}
public function cancelNotification($notificationId, $appId, $typeTarget)
{
if ($typeTarget == "D") {
$onesignalDelivery = $this->getOneSignalDelivery();
$onesignalDelivery->deleteNotification($notificationId, $appId);
} else {
OneSignal::deleteNotification($notificationId, $appId);
}
}
}