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

时间:2015-12-12 10:00:07

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

我正在按照Gooogle的教程进行操作:

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

我已经尝试但不知道如何在推送通知中获取数据。

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); 是我推送到Google Cloud Messaging的字段之一:

#include <stdio.h>

int main(void){
  char *temp = malloc(11);
  strcpy(temp, "123456789");
  free(temp);
  printf("%c\n", *temp);
  return 0;
}

1 个答案:

答案 0 :(得分:2)

event.data尚未在chrome中实现。它似乎实际上只是发送soon

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