PHP-Web Push Notifications (Implementation of web-push-php package)
A service worker can handle push notifications that can be shown even when the browser tab or the browser itself is closed. Implementing this using PHP, JS and HTML require a third party service. The plugin web-push-php can help this to happen. Clone the directory to your localhost area and check how it can be implemented.
- PHP 7.1+
- gmp
- mbstring
- curl
- opensslPHP 7.2+ is recommended for better performance.
There is no support and maintenance for older PHP versions, however you are free to use the following compatible versions:
- PHP 5.6 or HHVM:
v1.x
- PHP 7.0:
v2.x
Since we're using advanced browser properties like serviceWorker and PushManager, ensure that your browser supporting all these properties before start. Check the browser support here.
Use composer to download and install the library and its dependencies.
- Clone the directory to your localhost location.
- Run command
composer install
to install the required plugins. - Open http://localhost/php-service-worker-push-notification/ in your browser.
- Click Allow button if the browser show permission prompt.
- Go to the browser application tab to see the service worker file is registered. You can check the working by clicking the
Push
button in this section. - Click the
Get notification
link to get the subscription. Copy the subscription data from the textarea. - Send the subscription data to http://localhost/php-service-worker-push-notification/trigger using postman. Use
POST
method and send asform-data
by assigning to the keysubscription
.
- Use this link to generate the demo VAPIDs.
- Copy the
public key
andprivate key
and paste it in the code. - These VAPIDs has limitations and can use only for testing purpose.
trigger.php
$auth = array(
'VAPID' => array(
'subject' => 'http://localhost:8887/',
'publicKey' => 'YOUR_PUBLIC_KEY',
'privateKey' => 'YOUR _PRIVATE_KEY',
),
);
handler.js
const PUBLIC_KEY = YOUR_PUBLIC_KEY;
sw.js
const applicationServerPublicKey = YOUR_PUBLIC_KEY;
Generate VAPIDs from Google Firebase
- Login/Sign up to the Google Firebase
- Go to the console and create new project.
- Go to the Project settings. (Click the settings icon on the right side of the
Project overview
button to see theProject settings
option) - Navigate to the
Cloud Messaging
tab and find the keys fromWeb configuration
section. - Replace all the demo keys in the code and start working.