NodeJS 通过 firebase 将 APN 发送到应用程序

NodeJS sending APN to app through firebase

嗨,我想将 APN 发送到我的应用程序。我能够成功生成通知并将它们发送到我的应用程序。

我的问题是,服务器以块的形式频繁发送通知。我猜我的脚本算法有问题。

我想做的事情:

我希望每次 post 中有评论时都向设备发送通知。我想从 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();

要初始化您的应用程序,我认为最好使用您可以通过 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文件在你的函数文件夹中。

不过,我认为您需要稍微重构一下数据库,以便更容易地在服务器端检索您的 ID,例如,它可能看起来像这样:

root/
|     ....
|  
|
|___ posts/
|      |___ postID
|              |___ authorId : ...
|              |___ caption : ...
|              |___ comments
|                     |___ commentID 
|                           |___ senderId: ...
|                           |___ text: ...
|               ...
|
|
|___ users/
|      |___ userID
|              |___ name : ...
|              |___ email : ...
|              |___ notificationTokens
|                       |___ token1 : true                       
|                       |___ token2 : true
|                        ...

然后您可以创建使用函数在通知节点中写入通知对象,只要在您的评论节点中有新的写入事件:

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

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