Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用firebase云函数增加徽章计数_Javascript_Swift_Apple Push Notifications_Firebase Cloud Messaging_Google Cloud Functions - Fatal编程技术网

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

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

现在我的有效载荷只发送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
,并在每次调用时增加它,或者随机化它,或者做其他事情

即:


难道你不能只做一个增量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
            });
        };
    });
});