C. php / notifica.php

1
<?php
2
3
require_once __DIR__ . "/lib/manejaErrores.php";
4
require_once __DIR__ . "/../vendor/autoload.php";
5
require_once  __DIR__ . "/lib/devuelveJson.php";
6
require_once  __DIR__ . "/Bd.php";
7
require_once __DIR__ . "/Suscripcion.php";
8
require_once __DIR__ . "/suscripcionElimina.php";
9
10
use Minishlink\WebPush\WebPush;
11
12
const AUTH = [
13
 "VAPID" => [
14
  "subject" => "https://notipush.rf.gd/",
15
  "publicKey" => "BMBlr6YznhYMX3NgcWIDRxZXs0sh7tCv7_YCsWcww0ZCv9WGg-tRCXfMEHTiBPCksSqeve1twlbmVAZFv7GSuj0",
16
  "privateKey" => "vplfkITvu0cwHqzK9Kj-DYStbCH_9AhGx9LqMyaeI6w"
17
 ]
18
];
19
20
$webPush = new WebPush(AUTH);
21
$mensaje = "Hola! 👋";
22
23
// Envia el mensaje a todas las suscripciones.
24
25
$bd = Bd::pdo();
26
$stmt = $bd->query("SELECT * FROM SUSCRIPCION");
27
$suscripciones =
28
 $stmt->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, Suscripcion::class);
29
30
foreach ($suscripciones as $suscripcion) {
31
 $webPush->queueNotification($suscripcion, $mensaje);
32
}
33
$reportes = $webPush->flush();
34
35
// Genera el reporte de envio a cada suscripcion.
36
$reporteDeEnvios = "";
37
foreach ($reportes as $reporte) {
38
 $endpoint = $reporte->getRequest()->getUri();
39
 $htmlEndpoint = htmlentities($endpoint);
40
 if ($reporte->isSuccess()) {
41
  // Reporte de éxito.
42
  $reporteDeEnvios .= "<dt>$htmlEndpoint</dt><dd>Éxito</dd>";
43
 } else {
44
  if ($reporte->isSubscriptionExpired()) {
45
   suscripcionElimina($bd, $endpoint);
46
  }
47
  // Reporte de fallo.
48
  $explicacion = htmlentities($reporte->getReason());
49
  $reporteDeEnvios .= "<dt>$endpoint</dt><dd>Fallo: $explicacion</dd>";
50
 }
51
}
52
53
devuelveJson(["reporte" => ["innerHTML" => $reporteDeEnvios]]);
54
skip_previous skip_next