带有 PHP 后端的 Web 推送通知示例

kap*_*rda 5 notifications push web-push

我正在寻找一个带有 JS 代码和 PHP 后端的网络推送通知示例。任何人都可以分享示例代码或教程吗?

Min*_*ink 0

这是使用web-push-php 的基本示例:https://github.com/Minishlink/web-push-php-example

主要PHP代码是:

<?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
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 :)

  • 此示例不能开箱即用,需要更多说明。 (4认同)