我正在尝试了解GCM系统如何为chrome工作,但我找到的指南主要是在android和ios上,而我想在常规网站上使用网络通知。就像在任何常规通知模式中一样,我想在变量发生变化时在浏览器上收到通知,然后我想在我的脚本中使用新值。 我想知道是否有一个我可以收听的事件,比如onNotificationArrived() - 以javascript的方式 - 这将让我在我的浏览器和我的脚本中处理事件和有效负载/消息
**编辑:添加代码***
console.log('Started', self);
self.addEventListener('install', function(event) {
self.skipWaiting();
console.log('Installed', event);
});
self.addEventListener('activate', function(event) {
console.log('Activated', event);
});
self.addEventListener('push', function(event) {
console.log('Push Message', event);
var data = event.data;
console.log('data ',data);
var title = data.title;
console.log ('title ', title);
var custom= data['custom'];
console.log('custom ', custom);
});
PHP
<?php
require_once "connection.php";
$url = 'https://android.googleapis.com/gcm/send';
$apiKey = 'my_api_key';
$message = array(
'message' => 'test message',
'title' => 'test title',
'custom' => $myVar
);
$myVar = "34";
$registatoin_ids = array();
$selezione = "SELECT * FROM table";
$update = mysqli_query($conn, $selezione);
while ($row = $update->fetch_assoc()) {
array_push($registatoin_ids, $row["gcm_id"]);
echo($row["gcm_id"]);
}
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message
);
$test = json_encode($fields['data']);
echo($test);
$headers = array( 'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
//curl connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
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));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>