0
我創建了一個Push消息程序,它使用firebase.google.com。 如果通過JavaScript發送推送消息的消息是完美的,但如果我發送推送消息與Php捲曲消息不一樣,我寫的PHP文件。 如果我通過Php curl發送消息,那麼我將在service-worker.js中寫入消息。 這是爲什麼?通過curl錯誤的數據推送消息
PHP代碼
function send_push_message($subscription){
if (empty($subscription)) return FALSE;
$ch = curl_init();
switch ($subscription["browser"]){
case "firefox":
curl_setopt($ch, CURLOPT_URL, "https://updates.push.services.mozilla.com/push/".$subscription["id"]);
curl_setopt($ch, CURLOPT_PUT, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("TTL: 86400"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
break;
case "chrome":
/**
* https://console.firebase.google.com
* http://stackoverflow.com/questions/37427709/firebase-messaging-where-to-get-server-key
*/
// API access key from Google API's Console
define('API_ACCESS_KEY', '<API_SZERVER_KULCSOM>');
$registrationIds = array($_GET["id"]);
// prep the bundle
$msg = array
(
'message' => 'My text',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://gcm-http.googleapis.com/gcm/send");
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$response = curl_exec($ch);
echo $response;
if($response === false){
echo 'Hiba: '.curl_error($ch);
}else{
echo 'Siker';
}
curl_close($ch);
break;
}
}
$tomb["browser"] = "chrome";
$tomb["id"] = $_GET["id"];
if(isset($_GET["id"])){
send_push_message($tomb);
}
服務worker.js
self.addEventListener('push', function(event) {
var title = 'xxxxxx';
var body = 'Szerbusz YYY';
var icon = '/images/icon-192x192.png';
var tag = 'simple-push-demo-notification-tag';
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag
})
);
});
謝謝
_「的消息將是我在服務worker.js寫道:」 _因爲你的價值的是靜態的。 JS不會爲你自動替換這些值。你必須編寫代碼來設置事件數據有效載荷的主體,圖標等 –