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.31k stars 206 forks source link

Can't do anything within callback of sender.send #125

Closed l4zl0w closed 9 years ago

l4zl0w commented 9 years ago

I'm inexperienced with JS but I used other libraries and never met this problem before.

I'm trying to capture the messages that are successfully sent in the callback but I can't seem to access any variables from within the callback function of the send.

Is this an issue with this particular library or the problem is with my code?

var sendGCMNotifications = function(connectionObj, gcmNotifications) {
   var deliveredGCMNotificationIds = [];
   var platform = 'Android';
   var arrayLength = gcmNotifications.length;
   for(var i=0; i < arrayLength; i++) {
      gcmSender.send(gcmNotifications[i].message, gcmNotifications[i].registrationId, function(err, result){
         if(err) console.log(err);
         else {  //Nothing seems to be executed here!
           deliveredGCMNotificationIds.push(gcmNotifications[i].notificationId);
           if(i == arrayLength-1 && deliveredGCMNotificationIds.length > 0)
              updateNotificationStatus(connectionObj, deliveredGCMNotificationIds, platform);
           else console.log('nothing to deliver');
         }
      });
   }
};
hypesystem commented 9 years ago

What exactly happens when you run the code, and what did you expect to happen?

l4zl0w commented 9 years ago

I'm trying to capture the messages that were successfully sent. So I can update the statuses in my database. But I can't access the deliveredGCMNotificationIds array or the platform variable within the callback. Am I doing something wrong? Many thanks

l4zl0w commented 9 years ago

Figured it. Had to bind the arguments to the callback.