Firebase推送通知 – 节点工作人员

无论何时将某个子项添加到Firebasepath,我都需要向用户发送iOS推送通知。

我在想,做这件事的最好方法就是在Heroku上做一个Node.js工作人员,这个工作人员可以听取更改并使用Urban Airship发送通知。

我不确定最好的方法是从Heroku的Node.js工作人员那里听Firebase的变化。 我不熟悉heroku工人和Node.js。

任何人都可以给我一些指针? 例子?

使用Firebase和node-apn发送推送通知非常简单:

var apn = require("apn"); var Firebase = require("firebase"); // true for production pipeline var service = new apn.connection({ production: false }); // 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(); } 

要托pipe此脚本,您将无法使用Heroku之类的PaaS。 他们倾向于杀死空闲的sockets。 相反,你必须使用虚拟机。

Google Cloud Platform有一个运行良好的Node.js启动器。