Javascript 如何使用firebase云函数增加徽章计数
现在我的有效载荷只发送1个徽章计数,徽章计数的值不会增加。即使收到多个通知,它仍保持为1 javascript应该如何编写 我当前的代码:Javascript 如何使用firebase云函数增加徽章计数,javascript,swift,apple-push-notifications,firebase-cloud-messaging,google-cloud-functions,Javascript,Swift,Apple Push Notifications,Firebase Cloud Messaging,Google Cloud Functions,现在我的有效载荷只发送1个徽章计数,徽章计数的值不会增加。即使收到多个通知,它仍保持为1 javascript应该如何编写 我当前的代码: const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.sendPushForH
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
,并在每次调用时增加它,或者随机化它,或者做其他事情
即:
难道你不能只做一个增量int,然后将const有效载荷中的
badge
值设置为该值吗?你的意思是我应该在xcode(swiftcode)中包含增量int。然后我应该在有效载荷中使用这个变量。比方说badge:badgecount?这可能行得通,我甚至建议将var存储在JSF中,你能给我举个例子吗?我对JS很陌生。你应该在哪里声明变量?当然,我会在答案中快速写下来
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
});
};
});
});