Firebase推送通知 - 节点工作人员 [英] Firebase push notifications - node worker

查看:131
本文介绍了Firebase推送通知 - 节点工作人员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在想,做这件事的最好方法,就是将一个特定的孩子添加到Firebase路径中,就是在Heroku上做一个Node.js的工作人员,这个工作人员会听取更改,并使用Urban Airship发送一个通知。



我不确定最好的方法是什么在Heroku上监听来自Node.js工作人员的Firebase变更。我不熟悉heroku工作者和Node.js。

任何人都可以给我一些指点吗?例子?使用Firebase和node-apn发送推送通知很简单:


$ b

解决方案

pre> var apn = require(apn);
var Firebase = require(firebase);

//真正用于生产流水线
var service = new apn.connection({production:false});

//创建对推送通知队列的引用
var pushRef = new Firebase(< your-firebase> .firebaseio.com / notificationQueue);
//监听添加到队列中的项目
pushRef.on(child_added,function(snapshot){

//这个位置需要一个JSON对象:
// {
//token:String - 用户设备令牌
//message:String - 发送给用户的消息
//}
)var notificationData = snapshot.val();
sendNotification(notificationData);
snapshot.ref()。remove();

});

函数sendNotification(notificationData){
var notification = new apn.notification();
//默认的ping声音
notification.sound =ping.aiff;
//您的自定义消息
notification.alert = notificationData.message;
//将通知发送到特定的设备令牌
service.pushNotification(notification,[notificationData.token]);
//清理连接
service.shutdown();
}

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




I need to send iOS push notifications to user whenever a certain child is added to a Firebase path.

I was thinking, that the best way to do that, would be to make a Node.js worker on Heroku, that would listen for changes and send a notification using Urban Airship.

I'm not sure what the best way is to listen for changes on Firebase from a Node.js worker on Heroku is. I'm not that familiar with heroku workers and Node.js.

Can anyone give me some pointers? Examples?

解决方案

Sending push notifications with Firebase and node-apn is easy:

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();
}

For hosting this script, you won't be able to use a PaaS like Heroku. They tend to kill idle sockets. Instead you'll have to use a virtual machine.

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

这篇关于Firebase推送通知 - 节点工作人员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆