简体   繁体   English

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

[英]Chrome Push Notifications how to retrieve push data?

I'm following this tutorial by Gooogle: 我正在关注Gooogle的本教程:

https://developers.google.com/web/updates/2015/03/push-notifications-on-the-open-web https://developers.google.com/web/updates/2015/03/push-notifications-on-the-open-web

I've tried but don't know how to get the data in the push notification. 我已经尝试过,但不知道如何在推送通知中获取数据。

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  
    })  
  );  
});

For example, I want to retrieve data like this: 例如,我要检索这样的数据:

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  
    })  
  );  
});

which data is one of the fields that I pushed to Google Cloud Messaging: 哪些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 is not implemented in chrome yet. event.data尚未在Chrome中实现。 It is actually shipping pretty soon though it seems. 尽管看起来它实际上很快就发货了。

Note that once it ships you will be required to encrypt the data server side so it's going to be non trivial. 请注意,一旦发布,您将需要对数据服务器端进行加密,因此这将是不平凡的。 This is however important in order to hide the contents of the push message to the GCM server. 但是,这对于将推送消息的内容隐藏到GCM服务器很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM