Thursday, May 23, 2024
180
rated 0 times [  184] [ 4]  / answers: 1 / hits: 23891  / 9 Years ago, tue, may 19, 2015, 12:00:00

I'm working on the Google Chrome Push Notification and I'm trying to send the payload to the google chrome worker but, I have no idea how I receive this payload.



I have an API to create and save the notifications in my database and I need send the values through the https://android.googleapis.com/gcm/send and receive on the worker.js



This is my worker.js



    self.addEventListener('push', function(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
})
);
});


And this is how I'm calling the GCM



curl --header Authorization: key=AIzaSyDQjYDxeS9MM0LcJm3oR6B7MU7Ad2x2Vqc --header  Content-Type: application/json https://android.googleapis.com/gcm/send -d { data:{foo:bar}, registration_ids:[APA91bGqJpCmyCnSHLjY6STaBQEumz3eFY9r-2CHTtbsUMzBttq0crU3nEXzzU9TxNpsYeFmjA27urSaszKtA0WWC3yez1hhneLjbwJqlRdc_Yj1EiqLHluVwHB6V4FNdXdKb_gc_-7rbkYkypI3MtHpEaJbWsj6M5Pgs4nKqQ2R-WNho82mnRU]}


I tried to get event.data but, this is undefined.



Does anyone have any idea or sugestion?


More From » google-chrome

 Answers
19

To retrieve that data, you need to parse event.data.text() to a JSON object. I'm guessing something was updated since you tried to get this to work, but it works now. Unlucky!



However, since I made it to this post when searching for a solution myself, others would probably like a working answer. Here it is:



// Push message event handler
self.addEventListener('push', function(event) {

// If true, the event holds data
if(event.data){

// Need to parse to JSON format
// - Consider event.data.text() the stringify()
// version of the data
var payload = JSON.parse(event.data.text());
// For those of you who love logging
console.log(payload);

var title = payload.data.title;
var body = payload.data.body;
var icon = './assets/icons/icon.ico'
var tag = 'notification-tag';

// Wait until payload is fetched
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag,
data: {} // Keeping this here in case I need it later
})
);

} else {
console.log(Event does not have data...);
}

}); // End push listener

// Notification Click event
self.addEventListener('notificationclick', function(event) {
console.log(Notification Clicked);
}); // End click listener


Personally, I will be creating a generic notification in case my data is funky, and will also be using try/catch. I suggest doing the same.


[#66536] Monday, May 18, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breonnamayah

Total Points: 574
Total Questions: 115
Total Answers: 96

Location: England
Member since Sun, May 21, 2023
1 Year ago
breonnamayah questions
;