1
您好,我希望將APN發送到我的應用程序。我成功地能夠生成通知並將它們發送到我的應用程序。NodeJS通過Firebase將APN發送到應用程序
我的問題是,服務器在一個塊中相當頻繁地發送通知。我猜測我的腳本算法有問題。
我想要做什麼:
我想通知中每有一個在某條信息的評論時發送給設備。我想從Firebase數據庫中顯式獲取用戶名和評論。
我附上服務器腳本:
var firebase = require("firebase");
var once = require("once");
const apn = require('apn');
var config = {
apiKey: "<key>",
authDomain: "<domain>",
databaseURL: "<url>",
projectId: "<id>",
storageBucket: "<bucket>",
messagingSenderId: "<ID>"
};
firebase.initializeApp(config);
let options = {
token: {
key: "<p8 file>",
keyId: "<key>",
teamId: "<team>"
},
production: false
};
let apnProvider = new apn.Provider(options);
// Prepare the notifications
let notification = new apn.Notification();
notification.expiry = Math.floor(Date.now()/1000) + 24 * 3600; // will expire in 24 hours from now
notification.badge = 3;
notification.sound = "default";
notification.topic = "<My bundle ID>";
notification.payload = {'messageFrom': 'me'};
var author;
var dtoken;
var spotter;
var comment;
var database = firebase.database();
var postref = database.ref("posts").orderByChild("gen_notif").equalTo("yes").on("value", function (snapshot) {
var key;
var deviceToken;
console.log("-------------------Post Ids----------------------")
snapshot.forEach(function (childSnapshot) {
key = childSnapshot.key
author = childSnapshot.val()["author"];
console.log(key)
var newref = database.ref("posts/" + childSnapshot.key + "/comment").on('child_added', function(snapy){
console.log(snapy.val())
console.log("-----------------comment Keys----------------------")
snapy.forEach(function(s){
var spotuserkey = s.key
comment = s.val()
console.log(spotuserkey)
var spotuser = database.ref("users/"+ spotuserkey +"/credentials/name").on('value', function(spottersnap){
console.log("-----------------User Key-----------------------")
spotuser = spottersnap.val()
console.log(spotuser)
var tokenref = database.ref("device/"+author+"/token").once('value', function(snap){
console.log("-----------------device token---------------------")
deviceToken = snap.val()
console.log(deviceToken)
notification.alert = {
"title": "You Got Spotted",
"body": spotuser + " Spot you " + comment
};
apnProvider.send(notification, deviceToken).then(result => {
console.log(result["failed"][0]["response"]);
});
})//tokenref end
})//spotteref end
}); //snapy forEach end
})//newref end
}); //snapshot forEach end
}); //postref end
apnProvider.shutdown();
這看起來很完美。但我在劇本中犯了一個錯誤。感謝您的幫助。一旦我完成整合,我會更新你。乾杯隊友 –
太棒了!是的,只是讓我知道,如果一切正常,並隨時問我是否可以幫助你;)爲你的項目祝你好運! – Alex
嘿,我可能會遇到問題。我們在這裏使用雲功能。這似乎不適用於我。我得到'Firebase配置變量不可用。請使用最新版本的Firebase CLI部署此功能。儘管我已經安裝了Firebase CLI。我錯過了什麼嗎? –