Last active
April 20, 2021 17:19
-
-
Save davideast/b3fde097271a84a744a5 to your computer and use it in GitHub Desktop.
Send push notifications with node-apn and Firebase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Hi, I'm learning FCM - how can i do the same thing for Android?