1

現在我的有效負載僅發送1個徽章數量,徽章數量的值不會增加。它仍然是1,即使你得到超過1個通知。如何使用Firebase雲端功能增加徽章數量

javascript應該怎麼寫?

我當前的代碼:

const functions = require('firebase-functions');     
const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 
exports.sendPushForHelp = 
functions.database.ref('/help/school/{id}').onWrite(event => { 
    const payLoad = { 
    notification: { 
     title: 'Someone', 
     body: 'needs help', 
     badge: '1', 
     sound: 'Intruder.mp3' 
    } 
    }; 
    return admin.database().ref('fcmToken').once('value').then(allToken => { 
     if (allToken.val()) { 
     const token = Object.keys(allToken.val()); 
     return admin.messaging().sendToDevice(token, payLoad).then(response => { 

      }); 
     }; 
    }); 
}); 
+0

你不能只是做一個增量int,並設置const payLoad中的'badge'值等於那個? – Surreal

+0

你的意思是我應該在xcode中包含增量int(swiftcode)。然後我應該在有效載荷中使用該變量。讓我們說徽章:badgecount? – Lukayne

+0

這可以工作,我甚至建議存儲在JS中的var – Surreal

回答

1

您可以存儲在您的JS badgeCount並讓它在每次調用增加,或隨機,或你有什麼

即:

const functions = require('firebase-functions');     
const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 
var badgeCount = 1; 
exports.sendPushForHelp = 
functions.database.ref('/help/school/{id}').onWrite(event => { 
    const payLoad = { 
    notification: { 
     title: 'Someone', 
     body: 'needs help', 
     badge: badgeCount.toString(), 
     sound: 'Intruder.mp3' 
    } 
    }; 
    badgeCount++; //or whatever 
    return admin.database().ref('fcmToken').once('value').then(allToken => { 
     if (allToken.val()) { 
     const token = Object.keys(allToken.val()); 
     return admin.messaging().sendToDevice(token, payLoad).then(response => { 
      //here might be a good place to increment as well, only on success 
      }); 
     }; 
    }); 
}); 
相關問題