ToothlessGear / node-gcm

A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
https://github.com/ToothlessGear/node-gcm
Other
1.3k stars 206 forks source link

Service worker not able to read event #194

Closed ravikiran-gunale closed 8 years ago

ravikiran-gunale commented 8 years ago

While implementing push notifications using GCM, I sent message from server to GCM using node-gcm as per example in documentation of node-gcm. As per my understanding, 'event' parameter in addEventListener code (service-worker file), has above sent message data. But I am not seeing this message data in 'event' parameter. Is this a bug or error from my side?

eladnava commented 8 years ago

@ravikiran-gunale Can you please post code that reproduces this issue?

ravikiran-gunale commented 8 years ago
   Code from app.js file: (here I am calling GCM api and sending data to api)

    var message = new gcm.Message();
    message.addData('key1', 'msg1');
    message.addNotification('title', 'Alert!!!');
    message.addNotification('body', 'Abnormal data access');
    var sender = new gcm.Sender(website_key1);
    var registrationTokens = [];
    registrationTokens.push(registration_token);
  sender.send(message, { registrationTokens: registrationTokens }, function(err, response) {
    if(err) console.error(err);
  else    console.log(response);
  });

code from service-worker.js:

self.addEventListener('push', function(event) {  
var title = event.title;
var body = event.body;
var icon = '/images/image.png';  
var tag = 'simple-push-demo-notification-tag';
event.waitUntil(function() {
    self.registration.showNotification(title, {  
        body: body,  
        icon: icon,  
        tag: tag  
    })
  });

})

Here, I am able to send data to GCM api using node-gcm but in service-worker.js. event object in service-worker.js does not have data, title, body. Hence, I am getting an error

eladnava commented 8 years ago

@ravikiran-gunale You're describing a client-side bug.

node-gcm only handles sending the push notifications from your backend to GCM. Receiving and handling the notification on the client-side is your responsibility.