gpt4 book ai didi

javascript - 如何使用 firebase 云函数增加徽章计数

转载 作者:搜寻专家 更新时间:2023-10-31 22:26:50 25 4
gpt4 key购买 nike

现在我的负载只发送 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 并让它在每次调用时递增,或者随机化它或者你有什么

即:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com