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

标签 notifications push web-push

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

最佳答案

这是一个使用 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

希望这可以帮助 :)

关于notifications - 带有 PHP 后端的 Web 推送通知示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42746316/

相关文章:

android自定义通知不适用于约束布局

javascript - 如何使用 javascript/HTML 使通知闪烁

ios - FCM 在 iPhone 8 前台模式下无法接收

android - 推送通知开/关

git - 当 gitlab-CI 管道失败时阻止提交

java - Windows 上的虚假唤醒。是否可以?

ios - Git 无法识别 project.pbxproj

javascript - 在 Service Worker 中访问 Chrome API

c# - Asp Net Core Web 推送通知

node.js - 如何同时使用 "web-push"和 "fcm-push" Node 包?