I'm trying to use Apns library for node.js and get an error - "TypeError: Cannot read property 'bufferSize' of undefined"

I'm trying to use Apns library for node.js and get an error - "TypeError: Cannot read property 'bufferSize' of undefined"

在 Ubuntu Linux 我是 运行ning node.js 0.12.7 并且我已经安装了 npm 包 "apns"。我希望将推送通知发送到苹果设备,然后再发送到 Android 和 Windows。

我有非常基本的代码,但仍然 运行 遇到问题。脚本抛出以下 运行 时间错误:

  pankaj@pankaj-Ubuntu-asus:~/nodeServer/testCode$ node testPushDirectToApple.js
  [Wed Sep 09 2015 02:32:12 GMT-0400 (EDT)] Send : 6840038dee0ac6aa3c4a5210746961e80ed9797410a5100f8835b8dfbb
[Wed Sep 09 2015 02:32:12 GMT-0400 (EDT)] open socket
[Wed Sep 09 2015 02:32:12 GMT-0400 (EDT)] drain
 /home/pankaj/node_modules/apns/lib/connection.js:193
  if(this.tlsConnection && this.tlsConnection.socket.bufferSize !== 0)
                                                 ^
 TypeError: Cannot read property 'bufferSize' of undefined
    at Connection.drain (/home/pankaj/node_modules/apns/lib    /connection.js:193:54)
    at TLSSocket.<anonymous> (/home/pankaj/node_modules/apns/lib/connection.js:171:12)
    at TLSSocket.g (events.js:199:16)
    at TLSSocket.emit (events.js:104:17)
    at TLSSocket.<anonymous> (_tls_wrap.js:950:16)
    at TLSSocket.emit (events.js:104:17)
    at TLSSocket._finishInit (_tls_wrap.js:460:8)

这是我的代码:"testPushDirectToApple.js"

 var apns = require("apns"), notification, options, connection;

   options = {
       keyFile : "privateKey_key.pem",
       certFile : "aps_development_cert.pem",
       passphrase: "<scrubbed>",
       gateway: "gateway.sandbox.push.apple.com",
       port: 2195,
       debug : true
    };

 function pushviaDirectConenction(deviceToken, message){

  connection = new apns.Connection(options);

  notification = new apns.Notification();
  notification.alert = message;
  notification.device = new apns.Device(deviceToken);

  connection.sendNotification(notification);
}

  pushviaDirectConenction("68c2440038dee0ac6aa3c4a5210746961e80ed9797410a5100f8835b8d", "Hi there");

请指教如何解决这个问题。

提前致谢。

我遇到了同样的问题,我发现我使用了错误的库。

正确的是:https://github.com/argon/node-apn

要小心,因为还有一个名为 node-apns 的库,我认为它不会继续使用并且已弃用。

加载 pem 文件并 运行 粘贴令牌

var apn = require("apn");

var deviceToken = "device token";

var service = new apn.Provider({
    cert: '.path to /cert.pem', key:'pat to ./key.pem'
});

    var note = new apn.Notification();


  note.expiry = Math.floor(Date.now() / 1000) + 60; // Expires 1 minute from now.
  note.badge = 3;
  note.sound = "ping.aiff";
  note.alert = " You have a new message";
  note.payload = {'messageFrom': 'Rahul test apn'};
  note.topic = "(Bundle_id).voip";
  note.priority = 10;
  note.pushType = "alert";

  service.send(note, deviceToken).then( (err,result) => {
    if(err) return console.log(JSON.stringify(err));
    return console.log(JSON.stringify(result))
  });

这是 apn voip 推送的工作代码

也参考这个

https://alexanderpaterson.com/posts/send-ios-push-notifications-with-a-node-backend