| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/../vendor/autoload.php"; |
| 4 | require_once __DIR__ . "/../lib/php/ejecutaServicio.php"; |
| 5 | require_once __DIR__ . "/../lib/php/select.php"; |
| 6 | require_once __DIR__ . "/../lib/php/devuelveJson.php"; |
| 7 | require_once __DIR__ . "/Bd.php"; |
| 8 | require_once __DIR__ . "/TABLA_SUSCRIPCION.php"; |
| 9 | require_once __DIR__ . "/Suscripcion.php"; |
| 10 | require_once __DIR__ . "/suscripcionElimina.php"; |
| 11 | |
| 12 | use Minishlink\WebPush\WebPush; |
| 13 | |
| 14 | const AUTH = [ |
| 15 | "VAPID" => [ |
| 16 | "subject" => "https://notificacionesphp.gilbertopachec2.repl.co/", |
| 17 | "publicKey" => "BMBlr6YznhYMX3NgcWIDRxZXs0sh7tCv7_YCsWcww0ZCv9WGg-tRCXfMEHTiBPCksSqeve1twlbmVAZFv7GSuj0", |
| 18 | "privateKey" => "vplfkITvu0cwHqzK9Kj-DYStbCH_9AhGx9LqMyaeI6w" |
| 19 | ] |
| 20 | ]; |
| 21 | |
| 22 | ejecutaServicio(function () { |
| 23 | |
| 24 | $webPush = new WebPush(AUTH); |
| 25 | $mensaje = "Hola! 👋"; |
| 26 | |
| 27 | |
| 28 | |
| 29 | $pdo = Bd::pdo(); |
| 30 | |
| 31 | $suscripciones = select( |
| 32 | pdo: $pdo, |
| 33 | from: SUSCRIPCION, |
| 34 | mode: PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, |
| 35 | opcional: Suscripcion::class |
| 36 | ); |
| 37 | |
| 38 | foreach ($suscripciones as $suscripcion) { |
| 39 | $webPush->queueNotification($suscripcion, $mensaje); |
| 40 | } |
| 41 | $reportes = $webPush->flush(); |
| 42 | |
| 43 | |
| 44 | $reporteDeEnvios = ""; |
| 45 | foreach ($reportes as $reporte) { |
| 46 | $endpoint = $reporte->getRequest()->getUri(); |
| 47 | $htmlEndpoint = htmlentities($endpoint); |
| 48 | if ($reporte->isSuccess()) { |
| 49 | |
| 50 | $reporteDeEnvios .= "<dt>$htmlEndpoint</dt><dd>Éxito</dd>"; |
| 51 | } else { |
| 52 | if ($reporte->isSubscriptionExpired()) { |
| 53 | suscripcionElimina($pdo, $endpoint); |
| 54 | } |
| 55 | |
| 56 | $explicacion = htmlentities($reporte->getReason()); |
| 57 | $reporteDeEnvios .= "<dt>$endpoint</dt><dd>Fallo: $explicacion</dd>"; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | devuelveJson(["reporte" => ["innerHTML" => $reporteDeEnvios]]); |
| 62 | }); |
| 63 | |