2015-02-24 54 views
6

每当将某个子项添加到Firebase路径时,我需要向用户发送iOS推送通知。Firebase推送通知 - 节点工作人员

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

我不确定最好的方法是从Heroku上的Node.js工作人员监听Firebase上的更改。我不熟悉heroku工作者和Node.js。

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

+1

如何排队系统:推送通知对象至由服务器观看的位置,并使用事务处理,然后将其删除。有关示例实现,请参阅https://github.com/firebase/firebase-work-queue。 – 2015-02-24 06:43:07

+0

有趣。我会研究它。 – 2015-02-24 16:54:46

+0

这是一个我认为很酷的相关问题http://stackoverflow.com/questions/28617476/how-to-mitigate-against-long-startup-times-in-firebase-workers-when-dataset-gets – 2015-02-25 20:36:49

回答

4

发送推送通知与火力地堡和节点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(); 
} 

举办这个剧本,你将无法使用的PaaS像Heroku的。他们倾向于杀死空闲的套接字。相反,你必须使用虚拟机。

Google Cloud Platform has a Node.js launcher that works well.

+0

from the line:'' .firebaseio.com/notificationQueue“','notifiationQueue'是什么,或者只是引用来监听Firebase中的更改?另请注意,请参阅:“Google云平台有一个运行良好的Node.js启动程序。”,为什么不使用Firebase托管? – Sauron 2016-02-26 13:36:29

+0

1.是的,当推送通知需要发送到特定设备时,您就是这么听的。 2. Firebase托管仅适用于静态资产,因此没有服务器进程。 – 2016-02-26 14:13:23

+0

好的,那么这将解决我的问题。我正在学习以下教程:https://cloud.google.com/solutions/mobile/firebase-app-engine-android-studio,并在应用引擎中使用servlet,但它并没有实时监听firebase节点,因为应用程序引擎套接字保持关闭......,并且只能根据“cron.xml”文件进行调度。非常令人失望...... – Sauron 2016-02-26 14:16:48