Skip to content

Instantly share code, notes, and snippets.

@abdelmajidrajad
Forked from davideast/push.js
Created October 9, 2020 11:11
Show Gist options
  • Save abdelmajidrajad/7004d2c179dc0f541a112423b2bb90eb to your computer and use it in GitHub Desktop.
Save abdelmajidrajad/7004d2c179dc0f541a112423b2bb90eb 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