【发布时间】:2017-06-08 15:12:02
【问题描述】:
我想用 PHP 发送推送通知,但我不知道该怎么做。
我成功创建了 Service Worker。
我从这个页面https://web-push-codelab.appspot.com/拿了一把钥匙。但我不明白他们为什么要提供私钥?
然后我测试我的推送通知,并从这个页面发送一些通知。 但我想从我的 PHP 请求中发送推送通知。 更多,我会在所有可能的浏览器上工作,这个代码只适用于谷歌浏览器。我们可以用 firefox、safari、opéra 做到这一点吗?
我已经在使用 librairie web-push-php。 (可在此处获取:https://github.com/web-push-libs/web-push-php) 我从文档中复制/粘贴代码,然后替换我的密钥和消息:`
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
use Minishlink\WebPush\WebPush;
$notifications = array(
array(
'endpoint' => 'https://updates.push.services.mozilla.com/push/abc...', // Firefox 43+
'payload' => 'hello !',
'userPublicKey' => '*****', // base 64 encoded, should be 88 chars
'userAuthToken' => '*****', // base 64 encoded, should be 24 chars
), array(
'endpoint' => 'https://android.googleapis.com/gcm/send/abcdef...', // Chrome
'payload' => '{msg:"je fais un test"}',
'userPublicKey' => '*****',
'userAuthToken' => null,
)
);
$webPush = new WebPush();
// send multiple notifications with payload
foreach ($notifications as $notification) {
$webPush->sendNotification(
$notification['endpoint'],
$notification['payload'], // optional (defaults null)
$notification['userPublicKey'], // optional (defaults null)
$notification['userAuthToken'] // optional (defaults null)
);
}
$webPush->flush();
But this code don't work, the page return this :{"multicast_id":6971447349404987967,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}`
我也创建了一个 firebase 帐户,但我不知道如何使用它。
我还想针对通知。 我想发送通知,例如“xxx 对您的帖子发表评论”或“xxx 对您的帖子点赞”或“xxx 向您发送好友请求”
希望你能帮助我。
【问题讨论】:
-
您刚刚在公共论坛上发布了您的公钥和身份验证令牌。您应该认为您的帐户已被盗用并立即撤销该令牌。
标签: php push-notification web-push