【问题标题】:Web push notification example with PHP backend带有 PHP 后端的 Web 推送通知示例
【发布时间】:2017-08-02 10:43:39
【问题描述】:

我正在寻找一个带有 JS 代码和 PHP 后端的 Web 推送通知示例。谁能分享示例代码或教程?

【问题讨论】:

  • 如果您想使用服务,请查看Pushpad PHP library。从Getting started开始,然后你可以看到some examples关于收集订阅,最后你可以使用PHP库从你的PHP后端发送通知。
  • 谢谢,我会试试这个,如果你有任何关于 GCM 和 APN 的例子,请告诉我。
  • 你的意思是从头开始吗?因为在后台 Pushpad 使用 GCM、APN 和 Mozilla 自动推送。
  • 其实我正打算开一个类似pushpad的服务,所以我也在找一样的..

标签: notifications push web-push


【解决方案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

希望这会有所帮助:)

【讨论】:

  • 欢迎提供解决方案的链接,但请确保您的答案在没有它的情况下有用:add context around the link 这样您的其他用户就会知道它是什么以及为什么会出现,然后引用最相关的内容您链接到的页面的一部分,以防目标页面不可用。 Answers that are little more than a link may be deleted.
  • 这个例子不能开箱即用,需要更多说明。
  • @halfpastfour.am 这对于 SA 的回答来说太过分了,这就是它在 GitHub 存储库中的原因。
  • @t1gor 你误会我了。更多指令并不意味着整个代码库。
  • 正如其他人所说,需要更多的上下文。我不考虑这个完整的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 2017-11-12
  • 2021-06-11
  • 2018-04-04
  • 2015-09-14
  • 1970-01-01
相关资源
最近更新 更多