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