【问题标题】:Chrome Push Notifications how to retrieve push data?Chrome 推送通知如何检索推送数据?
【发布时间】:2016-03-18 06:02:52
【问题描述】:

我正在关注谷歌的这个教程:

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

【问题讨论】:

    标签: android notifications web-push push-api gmcs


    【解决方案1】:

    event.data 尚未在 chrome 中实现。尽管看起来,它实际上正在运送漂亮的soon

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多