使用PHP后端的Web推送通知示例

时间:2017-03-12 10:32:27

标签: notifications push web-push

我正在寻找一个使用JS代码和PHP后端进行Web推送通知的示例。任何人都可以共享示例代码或教程吗?

1 个答案:

答案 0 :(得分:1)

以下是使用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

希望这会有所帮助:)