【发布时间】:2018-02-23 08:31:08
【问题描述】:
您好,我想将 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();
【问题讨论】:
标签: ios node.js firebase-realtime-database apple-push-notifications