到目前为止,GCM只适用于chrome和android。
同样,firefox和其他浏览器也有自己的api。
现在的问题是,如何实现推送通知,使其适用于所有具有自己后端的常见浏览器。
你需要客户端脚本代码,即service worker,参考(谷歌推送通知)。尽管这对其他浏览器来说是一样的。
2.在使用Ajax获得端点后,将其与浏览器名称一起保存。
3.您需要创建后端,其中有字段的标题,消息,图标,点击URL根据您的需求。现在,在单击发送通知之后,调用函数send_push()。例如,在这里为不同的浏览器编写代码
3.1. 在chrome
$headers = array(
'Authorization: key='.$api_key(your gcm key),
'Content-Type: application/json',
);
$msg = array('to'=>'register id saved to your server');
$url = 'https://android.googleapis.com/gcm/send';
$ch = curl_init();
// Set the url, number of POST vars, POST data
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($msg));
$result = curl_exec($ch);
3.2. mozilla
$headers = array(
'Content-Type: application/json',
'TTL':6000
);
$url = 'https://updates.push.services.mozilla.com/wpush/v1/REGISTER_ID_TO SEND NOTIFICATION_ON';
$ch = curl_init();
// Set the url, number of POST vars, POST data
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);
$result = curl_exec($ch);
其他浏览器请谷歌…