File Manager
Editing: send-scheduled-push.php
<?php // Enable error reporting ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Load Composer's autoloader require __DIR__ . '/vendor/autoload.php'; use Minishlink\WebPush\WebPush; use Minishlink\WebPush\Subscription; // Database connection $host = "127.0.0.1:3306"; $username = "u404542307_dacotywebsites"; $password = "daCotywebs1te5"; $database = "u404542307_eizon"; $conn = new mysqli($host, $username, $password, $database); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // VAPID setup $webPush = new WebPush([ 'VAPID' => [ 'subject' => 'mailto:dacotyindustry@gmail.com', 'publicKey' => 'BHik82BfHN14SzCOfrZmCssN0nsFIoOBHi8l2hMBh9QLUTjtMXd87Xxbi2HMadLU-WuR9BvlM4Kd2ifS-5dkDg0', 'privateKey' => 'XeHu24YiC_ipTc3qPS0JluQmJ6I1n2CM0xSIVRtxNCU', ], ]); // Check notification type $type = $_GET['type'] ?? ''; // Define notification messages $messages = [ 'new_products' => [ 'title' => 'π Check out new products on eizon!', 'body' => 'New products have been added. Donβt miss out!', 'url' => '/home', 'condition' => true ], 'product_requests' => [ 'title' => 'π’ Check product requests on eizon!', 'body' => 'See what products people are searching for.', 'url' => '/requests_page', 'condition' => true ], 'buy_and_sell' => [ 'title' => 'π Buy and Sell on eizon!', 'body' => 'The Marketplace You Can Trust.', 'url' => '/home', 'condition' => true ] ]; if (!isset($messages[$type])) { exit('Invalid notification type'); } $message = $messages[$type]; if (!$message['condition']) { exit('Explore EIZON today!.'); } // Prepare the notification payload $notificationPayload = json_encode([ 'title' => $message['title'], 'body' => $message['body'], 'url' => $message['url'], ]); // Fetch all subscriptions from the database $subscriptions = []; $result = $conn->query("SELECT endpoint, p256dh, auth FROM push_subscriptions"); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $subscriptions[] = Subscription::create([ 'endpoint' => $row['endpoint'], 'keys' => [ 'p256dh' => $row['p256dh'], 'auth' => $row['auth'], ], ]); } } else { exit('No users subscribed to notifications.'); } // Send notifications to all subscribed users foreach ($subscriptions as $subscription) { $webPush->queueNotification($subscription, $notificationPayload); } // Process notifications $results = $webPush->flush(); foreach ($results as $report) { if ($report->isSuccess()) { echo "β Notification sent successfully to " . $report->getEndpoint() . "<br>"; } else { echo "β Notification failed to " . $report->getEndpoint() . ": " . $report->getReason() . "<br>"; } } $conn->close(); ?>
πΎ Save
β¬ Back