Skip to content

Instantly share code, notes, and snippets.

@novasush
Forked from davideast/push.js
Created June 17, 2019 06:07
Show Gist options
  • Save novasush/0b0e102162a0eff98e51e2430086b424 to your computer and use it in GitHub Desktop.
Save novasush/0b0e102162a0eff98e51e2430086b424 to your computer and use it in GitHub Desktop.
Send push notifications with node-apn and Firebase
var apn = require("apn");
var Firebase = require("firebase");
var service = new apn.connection({ production: false }); // true for production pipeline
// Create a reference to the push notification queue
var pushRef = new Firebase("<your-firebase>.firebaseio.com/notificationQueue");
// listen for items added to the queue
pushRef.on("child_added", function(snapshot) {
// This location expects a JSON object of:
// {
// "token": String - A user's device token
// "message": String - The message to send to the user
// }
var notificationData = snapshot.val();
sendNotification(notificationData);
snapshot.ref().remove;
});
function sendNotification(notificationData) {
var notification = new apn.notification();
// The default ping sound
notification.sound = "ping.aiff";
// Your custom message
notification.alert = notificationData.message;
// Send the notification to the specific device token
service.pushNotification(notification, [notificationData.token]);
// Clean up the connection
service.shutdown();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment