首先:我在托管的Web服务器上运行我的通知服务后端。因此,我无法从Github安装类似Web-Push-Apis的东西。
我已经设法使用this Site with a Google codelab发送通知。通过从那里进行一些js代码分析,我实现了向https://web-push-codelab.glitch.me/api/send-push-msg发送POST请求。这是执行成功请求的代码(我收到了Push):
<?php $ch = curl_init();
$posts =
array(
"subscription" => array(
"endpoint" => "https://fcm.googleapis.com/fcm/send/eSjh0WEJrr8:APA91bHZ27ZTNCI3jAz_cQ0zU7XJK48pJCfXYZBaNx6jSXE3x2Wp3NCh2bdj4KeOQrYTVfpDO9B5b8nD13AgZvtBjOEZrCADD30mtwBGxkH5DhpIAUGl3we95ksYxNn0dsI-2sqcfT5P",
"expirationTime" => null,
"keys" => array(
"p256dh"=>"BGmhdBvOcgU6yBeUtCiBoBDbG7lXEiOiRclS4_rviJi2OX-ji-9xKmoItsg30GoU6Rs-gHZUvqO6As3ZxUxAHP0",
"auth" =>"p9lkcCVolUAV2uUfYnhXlw"
)
),
"data" => $_GET["msg"],
"applicationKeys" => array(
"public" => "BCdiN5BM9evNa3pBIQbXMiq0l3k3mh0Z50yFo4LBsJqXQnh-MddYlPGhEE38urwJunmrcVNlF1VT3K8pZrFz8z4",
"private" => "e2gCLRWMf1wMa8khygzeNmr96o-YE21COMLaFqPvRAg"
)
);
curl_setopt($ch, CURLOPT_URL, "https://web-push-codelab.glitch.me/api/send-push-msg");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($posts));
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo($result);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);?>
密钥对来自glitch.me页面。订阅数组保存数据,Serviceworker Codelab给了我。我的Serviceworker看起来与代码实验室完全一样。
有人可以告诉我如何直接向FCM执行请求,而不是(ab)使用Codelab示例页面中的服务