现在我的有效载荷只发送 1 个徽章计数,徽章计数的值不会增加。即使您收到 1 个以上的通知,它仍为 1。

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 => {

            });
        };
    });
});

最佳答案

你可以在你的 JS 中存储一个 badgeCount 并让它在每次调用时递增,或者随机化它或者你有什么

IE:

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

关于javascript - 如何使用 Firebase 云功能增加徽章计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45328059/

10-12 04:05