簡體   English   中英

Firebase推送通知 - 節點工作者

[英]Firebase push notifications - node worker

我需要在將某個孩子添加到Firebase路徑時向用戶發送iOS推送通知。

我在想,最好的方法是在Heroku上創建一個Node.js工作人員,它會監聽變化並使用Urban Airship發送通知。

我不確定從Heroku上的Node.js工作者那里聽Firebase更改的最佳方法是什么。 我不熟悉heroku worker和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();
}

要托管此腳本,您將無法使用像Heroku這樣的PaaS。 他們傾向於殺死空閑的插座。 相反,你將不得不使用虛擬機。

Google Cloud Platform有一個運行良好的Node.js啟動器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM