Ios 节点通过firebase向应用程序发送APN

Ios 节点通过firebase向应用程序发送APN,ios,node.js,firebase-realtime-database,apple-push-notifications,Ios,Node.js,Firebase Realtime Database,Apple Push Notifications,嗨,我想将APN发送到我的应用程序。我已成功生成通知并将其发送到我的应用程序 我的问题是,服务器发送通知的频率相当高。我猜我的脚本算法有问题 我想做什么: 我希望每次在帖子中有评论时都将通知发送到设备。我想从Firebase数据库中显式获取用户名和注释 我正在附加服务器脚本: var firebase = require("firebase"); var once = require("once"); const apn = require('apn'); var config = { api

嗨,我想将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();
var firebase=require(“firebase”);
var once=要求(“一次”);
const apn=需要('apn');
变量配置={
apiKey:“,
authDomain:“”,
数据库URL:“”,
投射:“,
storageBucket:“”,
messagingSenderId:“
};
firebase.initializeApp(配置);
让选项={
代币:{
关键字:“,
密钥ID:“”,
团队ID:“
},
制作:假
};
设apnProvider=新的apn.Provider(选项);
//准备通知
let notification=new apn.notification();
notification.expiry=Math.floor(Date.now()/1000)+24*3600;//将在24小时后过期
通知标志=3;
notification.sound=“默认”;
notification.topic=“”;
notification.payload={'messageFrom':'me'};
var作者;
var dtoken;
var观测员;
var评价;
var database=firebase.database();
var postref=database.ref(“posts”).orderByChild(“gen_notif”).equalTo(“yes”).on(“value”,函数(快照){
var键;
var deviceToken;
console.log(“--------------Post ID--------------------”)
snapshot.forEach(函数(childSnapshot){
key=childSnapshot.key
author=childSnapshot.val()[“author”];
console.log(键)
var newref=database.ref(“posts/”+childSnapshot.key+“/comment”)。关于('child_added',函数(snapy){
console.log(snapy.val())
console.log(“--------------注释键------------------”)
snapy.forEach(函数){
var spotuserkey=s.key
注释=s.val()
console.log(spotuserkey)
var spotuser=database.ref(“users/”+spotuserkey+“/credentials/name”)。on('value',函数(spottersnap){
console.log(“--------------用户密钥--------------------”)
spotuser=spottersnap.val()
console.log(spotuser)
var tokenref=database.ref(“设备/”+author+“/token”)。一次('value',函数(snap){
console.log(“--------------设备令牌-----------------”)
deviceToken=snap.val()
console.log(deviceToken)
notification.alert={
“标题”:“你被发现了”,
“body”:spotuser+“spotyou”+注释
};
发送(通知,deviceToken)。然后(结果=>{
console.log(结果[“失败”][0][“响应”]);
});
})//令牌引用端
})//点状端
});//每端都很快
})//新参考端
});//每个前端的快照
}); //后参考端
apnProvider.shutdown();

要初始化应用程序,我认为最好使用可以通过Firebase控制台下载的serviceAccountKey文件,而不是直接将密钥写入代码中:

const functions = require('firebase-functions');

var admin = require('firebase-admin');

var serviceAccount = require("./serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "YOUR_DATABASE_URL_GOES_HERE"
});
当然,您需要在此处替换为您自己的数据库url,并确保serviceAccountKey.json文件位于functions文件夹中

但是,我认为您需要稍微重新构造数据库,以便更容易在服务器端检索您的ID,例如,它可能如下所示:

root/
|     ....
|  
|
|___ posts/
|      |___ postID
|              |___ authorId : ...
|              |___ caption : ...
|              |___ comments
|                     |___ commentID 
|                           |___ senderId: ...
|                           |___ text: ...
|               ...
|
|
|___ users/
|      |___ userID
|              |___ name : ...
|              |___ email : ...
|              |___ notificationTokens
|                       |___ token1 : true                       
|                       |___ token2 : true
|                        ...
然后,只要comments节点中有新的写入事件,就可以创建并使用该函数在通知节点中写入通知对象:

exports.createPostCommentedNotification = functions.database.ref('/posts/{postID}/comments/{commentID}').onWrite(event => {

   const data = event.data;

   if(data == undefined || !data.val()) { return; }

    const postID = event.params.postID; 
    const commentID = event.params.commentID; 

    const getCommentSender = admin.database().ref(`/posts/${postID}/comments/${commentID}`).once('value');

    const getPostAuthor = admin.database().ref(`/posts/${postID}`).once('value');

    return Promise.all([getCommentSender, getPostAuthor]).then(results => {

       const commentSenderData = results[0];
       const postAuthorData = results[1];

       const commentSenderId = commentSenderData.val().senderId;
       const postAuthorId = postAuthorData.val().authorId;

       if(commentSenderId == postAuthorId) { 
          return;
       };

       const notificationID = admin.database().ref().push().key;
       const timestamp = Date.now()

       const getSenderProfilePromise = admin.auth().getUser(commentSenderId);

       return Promise.all([getSenderProfilePromise]).then(results => {

            // Note that when you create a user account you would need to set the displayName of the user using the updateProfile() method, otherwise you would need to retrieve the senderName in a different way:)

            const senderData = results[0]
            const senderName = senderData.providerData[0].displayName

            var notificationData = {
             senderName: senderName,
             notificationTimestamp: timestamp
            };

            var updates = {};

            updates['/notifications/' + postAuthorId + '/' + notificationID] = notificationData;

            admin.database().ref().update(updates);

            });
      });
});
然后,您将创建另一个函数,以便在通知节点中添加新的通知对象时,使用您的用户令牌实际发送推送通知,如下所示:

exports.sendPushNotifications = functions.database.ref('/notifications/{receiverId}/{notificationId}').onWrite(event => {

  const data = event.data;

  if(data == undefined || !data.val()) { return; }

  const receiverId = event.params.receiverId;
  const notificationId = event.params.notificationId;

  const getDeviceTokensPromise = admin.database().ref(`/users/${receiverId}/notificationTokens`).once('value');

  const getMessageContentPromise = admin.database().ref(`/notifications/${receiverId}/${notificationId}/notificationType`).once('value');

  const getSenderPromise = admin.database().ref(`/notifications/${receiverId}/${notificationId}/senderName`).once('value');

  return Promise.all([getDeviceTokensPromise, getSenderPromise]).then(results => {

    const tokensSnapshot = results[0];
    const senderSnapshot = results[1];

    const sender = senderSnapshot.val()

    if (!tokensSnapshot.hasChildren()) {
      return console.log('There are no notification tokens to send to.');
    }

    const payload = {
      notification: {
        title: `${sender}`,
        body: 'Someone commented on your post',
        badge: '1'
      }
    };

    var options = {
      priority: "high",
      timeToLive: 60 * 60 * 24,
      mutable_content : true,
      content_available : true,
      category : 'reminder'
    };

    const tokens = Object.keys(tokensSnapshot.val());

    return admin.messaging().sendToDevice(tokens, payload, options).then(response => {


      const tokensToRemove = [];
      response.results.forEach((result, index) => {
        const error = result.error;
        if (error) {
          console.error('Failure sending notification to', tokens[index], error);

          if (error.code === 'messaging/invalid-registration-token' ||
              error.code === 'messaging/registration-token-not-registered') {
            tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
          }
        }
      });
      return Promise.all(tokensToRemove);
    });
  });
});

如果你有任何问题,请告诉我

这看起来很完美。不过我的剧本出了个错误。谢谢你的帮助。完成集成后,我将更新您。干杯,太棒了!是的,只要让我知道一切是否顺利,并随时问我是否可以帮助你;)祝你的项目好运!嘿,我可能在一个问题上卡住了。我们在这里使用云函数。这似乎对我不起作用。我发现
Firebase配置变量不可用。请使用最新版本的Firebase CLI部署此功能。
。尽管我已经安装了firebase CLI。我遗漏了什么吗?我正在使用cloud9,但它似乎没有安装firebase CLI。希望你对我的两个问题都有一个解决方案。嗯,当你在终端中输入npm-v时,你会得到什么?