2017-09-14 27 views
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(); 

回答

1

要初始化您的應用程序,我認爲這將更好地利用你可以通過火力地堡控制檯下載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); 
    }); 
    }); 
}); 

那麼讓我知道如果您有任何問題!

+0

這看起來很完美。但我在劇本中犯了一個錯誤。感謝您的幫助。一旦我完成整合,我會更新你。乾杯隊友 –

+0

太棒了!是的,只是讓我知道,如果一切正常,並隨時問我是否可以幫助你;)爲你的項目祝你好運! – Alex

+0

嘿,我可能會遇到問題。我們在這裏使用雲功能。這似乎不適用於我。我得到'Firebase配置變量不可用。請使用最新版本的Firebase CLI部署此功能。儘管我已經安裝了Firebase CLI。我錯過了什麼嗎? –

相關問題