I am looking for an example for web push notification with JS code and PHP backend. Can anyone share example code or a tutorial?
Asked
Active
Viewed 1.2k times
6
-
If you want to use a service, check out the [Pushpad PHP library](https://github.com/pushpad/pushpad-php). Start from the [Getting started](https://pushpad.xyz/docs/pushpad_pro_getting_started), then you can see [some examples](https://pushpad.xyz/docs/javascript_sdk_examples) about collecting subscriptions, finally you can send notifications from your PHP backend with the PHP library. – collimarco Mar 13 '17 at 09:09
-
Thanks, i will try this, if you have any example with GCM and APN please let me know. – kapilkarda Mar 13 '17 at 10:52
-
Do you mean from scratch? Because under the hood Pushpad uses GCM, APNs and Mozilla autopush. – collimarco Mar 13 '17 at 19:54
-
Actually, i am planning to start a service similar to pushpad, thats why i am looking for the same.. – kapilkarda Mar 14 '17 at 17:12
2 Answers
0
Here's a basic example that uses web-push-php : https://github.com/Minishlink/web-push-php-example
Main PHP code is:
<?php
require __DIR__ . '/vendor/autoload.php';
use Minishlink\WebPush\WebPush;
$auth = array(
'VAPID' => array(
'subject' => 'https://github.com/Minishlink/web-push-php-example/',
'publicKey' => 'BCmti7ScwxxVAlB7WAyxoOXtV7J8vVCXwEDIFXjKvD-ma-yJx_eHJLdADyyzzTKRGb395bSAtxlh4wuDycO3Ih4',
'privateKey' => 'HJweeF64L35gw5YLECa-K7hwp3LLfcKtpdRNK8C_fPQ', // in the real world, this would be in a secret file
),
);
$webPush = new WebPush($auth);
$res = $webPush->sendNotification(
$subscription['endpoint'],
"Hello!", // payload
$subscription['key'],
$subscription['token'],
true // flush
);
// handle eventual errors here, and remove the subscription from your server if it is expired
Hope this helps :)

Minishlink
- 140
- 6
-
1A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](http://meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted](http://stackoverflow.com/help/deleted-answers). – thewaywewere May 06 '17 at 14:01
-
4This example does not work out of the box and needs more instructions. – halfpastfour.am Jul 25 '17 at 07:37
-
@halfpastfour.am that would way too much for SA answer, that's why it's in a GitHub repo. – t1gor Oct 15 '18 at 13:19
-
@t1gor you're misunderstanding me. More instructions does not mean the entire codebase. – halfpastfour.am Oct 15 '18 at 18:59
-
As others said, much more context is needed. I dont consider this full answer. – The Vojtisek Feb 11 '19 at 08:56
0
- You should generate VAPID keys.
- Than you should create "service-worker.js"
- After that you should get keys ('endpoint', 'p256dh', 'auth') for specify user
- And after that you may send push notification for the user using PHP.
Here you can find example "service-worker.js" and PHP script.

Ikenitenine
- 63
- 2
- 2
- 17