【问题标题】:How to increment badge count with firebase cloud functions如何使用 Firebase 云功能增加徽章计数
【发布时间】:2018-01-01 20:11:49
【问题描述】:

现在我的有效负载只发送 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 => {

            });
        };
    });
});

【问题讨论】:

  • 你不能只做一个增量 int 并将 const payLoad 中的 badge 值设置为等于那个值吗?
  • 您的意思是我应该在 xcode (swiftcode) 中包含增量 int。然后我应该在有效载荷中使用该变量。让我们说徽章:badgecount?
  • 这可行,我什至建议将 var 存储在 JS 中
  • 你能给我举个例子吗?我对 JS 很陌生。你应该在哪里声明变量?
  • 确定我会在答案中快速写下

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2021-11-07
    • 2021-02-19
    • 2021-03-01
    • 1970-01-01
    • 2013-09-29
    • 2019-07-19
    相关资源
    最近更新 更多