Chrome推送通知如何检索推送数据



我正在关注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是我推送给谷歌云消息的字段之一:

$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中实现。虽然看起来很快就会发货。

请注意,一旦发货,您将需要对数据服务器端进行加密,因此这将是非常重要的。然而,为了隐藏到GCM服务器的推送消息的内容,这一点非常重要。

相关内容

  • 没有找到相关文章

最新更新