如何使用firebase云功能增加徽章数量

时间:2017-07-26 13:11:41

标签: javascript swift apple-push-notifications firebase-cloud-messaging google-cloud-functions

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

如何编写javascript?

我目前的代码:

String scheme = request.getScheme();
String userInfo = request.getRemoteUser();
String host = request.getLocalAddr();
int port = request.getLocalPort();
String path = "/app2/myservice";
String query = "par=1";
URI uri = new URI(scheme, userInfo, host, port, path, query, null);
boolean isOK = restTemplate.getForObject(uri, Boolean.class);
if (isOK) {
    System.out.println("Is OK");
}

1 个答案:

答案 0 :(得分:3)

你可以在你的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
            });
        };
    });
});