node.js notifications apple-push-notifications push

node.js - ¿Cómo implementar notificaciones APNS a través de nodejs?



notifications apple-push-notifications (0)

¿Alguien tiene ahora un buen módulo de npm para implementar las notificaciones de Apple PUSH? Un simple ejemplo sería genial.

La solución que he encontrado es la siguiente que usa el módulo apn .

var apn = require(''apn''); var ca = [''entrust_2048_ca.cer'']; /* Connection Options */ var options = { cert: ''path to yuour cert.pem'', key: ''path to your key.pem'', ca: ca, passphrase: ''your passphrase'', production: true, connectionTimeout: 10000 }; var apnConnection = new apn.Connection(options); /* Device */ var deviceToken = ''your device token''; var myDevice = new apn.Device(deviceToken); /* Notification */ var note = new apn.Notification(); note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now. note.badge = 3; note.payload = {''message'': ''hi there''}; apnConnection.pushNotification(note, myDevice);

Si necesita utilizar el servicio de comentarios APNS para evitar enviar notificaciones a dispositivos que no pueden recibirlo (desinstalación de la aplicación), puede agregar lo siguiente:

/* Feedback Options */ var feedbackOptions = { cert: ''path to yuour cert.pem'', key: ''path to your key.pem'', ca: ca, passphrase: ''your passphrase'', production: true, interval: 10 }; var feedback = new apn.feedback(feedbackOptions); feedback.on(''feedback'', handleFeedback); feedback.on(''feedbackError'', console.error); function handleFeedback(feedbackData) { var time, device; for(var i in feedbackData) { time = feedbackData[i].time; device = feedbackData[i].device; console.log("Device: " + device.toString(''hex'') + " has been unreachable, since: " + time); } }

Para manejar los diferentes eventos conectados a la conexión, puede usar lo siguiente:

apnConnection.on(''connected'', function(openSockets) { // }); apnConnection.on(''error'', function(error) { // }); apnConnection.on(''transmitted'', function(notification, device) { // }); apnConnection.on(''transmissionError'', function(errCode, notification, device) { // }); apnConnection.on(''drain'', function () { // }); apnConnection.on(''timeout'', function () { // }); apnConnection.on(''disconnected'', function(openSockets) { // }); apnConnection.on(''socketError'', console.error);