我正在学习 Gooogle 的教程:
https://developers.google.com/web/updates/2015/03/push-notifications-on-the-open-web
我试过了,但不知道如何获取推送通知中的数据。
self.addEventListener('push', function(event) {
console.log('Received a push message', event);
var title = 'Yay a message.';
var body = 'We have received a push message.';
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
})
);
});
例如,我想像这样检索数据:
self.addEventListener('push', function(event) {
var data = event.data;
var title = data.title;
var body = data.body;
var icon = data.icon;
var tag = data.tag;
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag
})
);
});
哪个 data
是我推送到 Google Cloud Messaging 的字段之一:
$apiKey = '12345678';
$url = 'https://android.googleapis.com/gcm/send';
$post = array(
'registration_ids' => $ids,
'data' => $data,
'notification' => $notification,
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
$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_POSTFIELDS, json_encode($post));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'GCM error: ' . curl_error($ch);
}
curl_close($ch);
最佳答案
event.data 还没有在 chrome 中实现。它实际上很漂亮 soon虽然看起来。
请注意,一旦它发布,您将需要加密数据服务器端,因此这将是非常重要的。然而,为了将推送消息的内容隐藏到 GCM 服务器,这很重要。
关于android - Chrome 推送通知如何检索推送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34238542/