Firebase推送通知-节点工作程序



每当某个孩子被添加到Firebase路径时,我都需要向用户发送iOS推送通知。

我在想,最好的方法是在Heroku上创建一个Node.js工作程序,它会监听更改并使用Urban Airship发送通知。

我不确定从Heroku上的Node.js工作人员那里监听Firebase上的更改的最佳方式是什么。我对Heroku工作人员和Node.js不太熟悉。

有人能给我一些建议吗?示例?

使用Firebase和节点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();
}

对于托管这个脚本,您将无法使用像Heroku这样的PaaS。它们往往会杀死空闲的套接字。相反,您将不得不使用虚拟机。

谷歌云平台有一个Node.js启动器,运行良好。

最新更新