Reputation: 671
I'm following this tutorial by Gooogle:
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:
$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);
Upvotes: 1
Views: 878
Reputation: 1029
event.data is not implemented in chrome yet. 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.
Upvotes: 2