0

I have a node-apn nodejs script running as a daemon on AmazonWS. The daemon runs fine and the script stays up and comes back when it goes down but I believe I am having a synchronous execution and exiting issue with node.js. When I release the process with process.exit(); even though all console.logs output saying they have sent my messages, they never are received on the phone. I decided to remove the exit and let the process "hang" after execution and all messages were sent successfully. This led me to do the following implementation using an ASYNC function, but the same result seems to be happening. Can anyone provide insight to this? There are no errors being thrown from APN or anywhere else.

function closeDB()
{ 

    connection.end(function(err) {
    if (err) {
        console.log("ERROR: " + util.inspect(err, false, 5));
        process.exit(1);
    } 

    console.log("APNS-PUSH: COMPLETED.");
});

setTimeout(function(){process.exit();}, 50);

} // End of closeDB()

function apnsError(err, notification)
{
console.log(err);
console.log(notification);
closeDB();
}  

function async(arg, callback) 
{
apnsConnection.sendNotification(arg);
console.log(arg); 
setTimeout(function() { callback(1); }, 100);
} 

/**
* Our MySQL query callback.
*/
function queryCB(err, results) 
{

//error in our all, report and exit
if (err) {
    console.log("ERROR: " + util.inspect(err, false, 5));
    closeDB();
} 

if(results.length == 0)
{
    closeDB();
}

var notes = [];
var count = 0;

try {
    for( var i = 0; i < results.length; i++ ) {
        var myDevice = new apns.Device(results[i]['udid']);

        var note = new apns.Notification();

        note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
        note.badge = results[i]["notification_count"];
        note.sound = "ping.aiff";
        note.alert = results[i]["message"];
        note.device = myDevice;

        connection.query('UPDATE `tbl_notifications` SET `sent`=1 WHERE `id`=' + results[i]["id"] , function(err, results) {
                if(err)
                {
                    console.log("ERROR: " + util.inspect(err, false, 5));
                }
            });

        notes.push(note);
    }
} catch( err ) {
    console.log('error: ' + err)
}

console.log(notes.length);

notes.forEach(function(nNode) {
    async(nNode, function(result) {
        count++;
        if(count == notes.length) {
            closeDB();
        }
    })
});         

} // End of queryCB()
4

2 回答 2

0

我有同样的问题,杀死进程也杀死了打开的套接字连接并且不允许发送通知。我提出的解决方案不是一个理想的解决方案,但它也适用于您的情况。我查看了node-apn代码,发现 Connection 对象继承自EventEmitter,因此您可以像这样监视对象上的事件:

var apnsConnection = new apn.Connection(options)

apnsConnection.sendNotification(notification)

apnsConnection.on('transmitted', function(){
    console.log("Transmitted")
    callback()
})

apnsConnection.on('error', function(){
    console.log("Error")
    callback()
})

这是监视发送通知的套接字,所以我不知道它在确定通知何时成功传递到 Apple 的 APNS 服务器时有多准确,但它对我来说效果很好。

于 2013-05-30T18:28:05.750 回答
0

您看到此问题的原因是,当您使用#pushNotification它时,它会在模块内缓冲通知并处理异步发送。

侦听“已传输”是有效的,并且在将通知写入套接字时发出。但是,如果您的目标是在发送所有通知后关闭套接字,那么完成此操作的最简单方法是connectionTimeout在创建连接时使用该属性。

只需设置connectionTimeout为 1000(毫秒)左右,并假设您没有打开其他连接,那么该过程将自动退出。或者您可以在事件上设置事件侦听器并从那里timeout调用。process.exit()

于 2013-06-05T14:03:19.087 回答