File: PNTestServer.php

Recommend this page to a friend!
  Classes of Stefan Kientzler  >  PHP Web Push Notifications Server  >  PNTestServer.php  >  Download  
File: PNTestServer.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Web Push Notifications Server
Queue and push notifications to Web users
Author: By
Last change:
Date: 2 months ago
Size: 3,980 bytes
 

Contents

Class file image Download
<?php
use lib\PNServer\PNDataProvider;
use
lib\PNServer\PNDataProviderSQLite;
use
lib\PNServer\PNDataProviderMySQL;
use
lib\PNServer\PNSubscription;
use
lib\PNServer\PNVapid;
use
lib\PNServer\PNPayload;
use
lib\PNServer\PNServer;

require_once
'lib/PNServer/PNDataProviderSQLite.php';
require_once
'lib/PNServer/PNDataProviderMySQL.php';
require_once
'lib/PNServer/PNServer.php';

   
// check, if PHP version is sufficient and all required extensions are installed
   
$bExit = false;
    if (
version_compare(phpversion(), '7.1', '<')) {
       
trigger_error('At least PHP Version 7.1 is required (current Version is ' . phpversion() . ')!', E_USER_WARNING);
       
$bExit = true;
    }
   
$aExt = array('curl', 'gmp', 'mbstring', 'openssl', 'bcmath');
    foreach (
$aExt as $strExt) {
        if (!
extension_loaded($strExt)) {
           
trigger_error('Extension ' . $strExt . ' must be installed!', E_USER_WARNING);
           
$bExit = true;
        }
    }
    if (
$bExit) {
        exit();
    }

   
// for test use SQLite database - will be cretaed at first instantiation
   
$oDP = new PNDataProviderSQLite();
   
   
// or may use any MySQL database
    // $oDP = new PNDataProviderMySQL('localhost', 'username', 'password', 'db-name');
   
   
if (!$oDP->isConnected()) {
        exit();
    }

   
// just add expired (unsubscribed) subscription to demonstrate response of
    // push service and the auto-remove option
   
$oDP->saveSubscription(
           
'{'
           
.' "endpoint": "https://fcm.googleapis.com/fcm/send/f8PIq7EL6xI:APA91bFgD2qA0Goo_6sWgWVDKclh5Sm1Gf1BtYZw3rePs_GHqmC9l2N92I4QhLQtPmyB18HYYseFHLhvMbpq-oGz2Jtt8AVExmNU9R3K9Z-Gaiq6rQxig1WT4ND_5PSXTjuth-GoGggt",'
           
.' "expirationTime": "1589291569000",'
           
.' "keys": {'
           
.' "p256dh": "BEQrfuNX-ZrXPf0Mm-IdVMO1LMpu5N3ifgcyeUD2nYwuUhRUDmn_wVOM3eQyYux5vW2B8-TyTYco4-bFKKR02IA",'
           
.' "auth": "jOfywakW_srfHhMF-NiZ3Q"'
           
.' }'
           
.'}'
   
);
   
    echo
'Count of subscriptions: ' . $oDP->count() . '<br/><br/>' . PHP_EOL;
    if (
$oDP->init()) {
        while ((
$strJsonSub = $oDP->fetch()) !== false) {
            echo
'UA: ' . $oDP->getColumn(PNDataProvider::COL_USERAGENT);
            echo
' (Lastupdated: ' . date('Y-m-d H:i:s', $oDP->getColumn(PNDataProvider::COL_LASTUPDATED)) . ')<br/>' . PHP_EOL;
           
$strPrintable = json_encode(json_decode($strJsonSub), JSON_PRETTY_PRINT);
           
$strPrintable = str_replace("\n", '<br/>', $strPrintable);
           
$strPrintable = str_replace(" ", '&nbsp;', $strPrintable);
            echo
'<span style="font-size: 10pt; font-family: courier; overflow:scroll; white-space: nowrap">' . $strPrintable . '</span><br/><br/>' . PHP_EOL;
        }
    }
   
   
// the server to handle all
   
$oServer = new PNServer($oDP);

   
// set the VAPID key
   
$oVapid = new PNVapid(
           
"mailto:yourmail@yourdomain.de",
           
"the-generated-public-key",
           
"the-generated-private-key"
   
);
   
$oServer->setVapid($oVapid);
   
   
// create payload
    // - we don't set a title - so service worker uses default
    // - URL to icon can be
    // * relative to the origin location of the service worker
    // * absolute from the homepage (begining with a '/')
    // * complete URL (beginning with https://)
   
$oPayload = new PNPayload('', "...first text to display.", './elephpant.png');
   
// set tag to group the notifications but always show the popup
   
$oPayload->setTag('news', true);
   
// and lead the user to thr page of your choice
   
$oPayload->setURL('/where-to-go.php');
   
   
$oServer->setPayload($oPayload);

   
// load subscriptions from database (incluing the expired one created above...)
   
$oServer->loadSubscriptions();
   
   
// ... and finally push !
   
if (!$oServer->push()) {
        echo
'<h2>' . $oServer->getError() . '</h2>' . PHP_EOL;
    } else {
       
$aLog = $oServer->getLog();
        echo
'<h2>Push - Log:</h2>' . PHP_EOL;
        foreach (
$aLog as $strEndpoint => $aMsg ) {
            echo
'<h3>' . PNSubscription::getOrigin($strEndpoint) . '</h3>' . PHP_EOL;
            echo
$aMsg['msg'] . '<br/>resonse code: ' . $aMsg['curl_response_code'] . ' (' . $aLog['curl_response'] . ')';
        }
    }


For more information send a message to info at phpclasses dot org.