Flutter/Firebase 如何增加应用程序图标的徽章数量

Dae*_*Pyo 7 firebase flutter firebase-cloud-messaging

我是在加拿大学习计算机编程的学生。现在,我正在使用 flutter/firebase 将该应用程序作为我的副项目。

我的应用程序包含聊天功能。我面临的问题是如何增加徽章编号并在未阅读消息时保留它。

我附上了 firebase 中消息数据结构的图片。

下面的代码是 firebase 函数代码。现在我正在使用此代码推送通知。我成功收到通知,但问题是如何计算应用程序图标的徽章数量。正如您在通知徽章中看到的,编号为“1”。每当我推送通知时,该数字就会覆盖之前的数字。我认为这是错误的方式,这是我所期望的,如果值 isMessageRead 在 firebase 数据库增量徽章编号中。我只是想知道逻辑方式,或者我可以研究的任何其他来源可以解释如何计算图标徽章编号。

目前我不知道如何管理徽章号码。

谢谢阅读,

我正在等待你的回复。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();


exports.onCreateMessage = functions.firestore.document('/Chat/{currentUserID}/UserList/{anotherUserID}/Messages/{message}')
    .onCreate(async (snap, context) => {
        const currentUserID = context.params.currentUserID;
        const anotherUserID = context.params.anotherUserID;
        console.log(currentUserID);
        console.log(anotherUserID);
        if (currentUserID == snap.data().recieverID) {
            return admin.messaging().sendToTopic(`${currentUserID}`, {
                notification: {
                    title: snap.data().senderID,
                    body: snap.data().message,
                    clickAction: 'FLUTTER_NOTIFICATION_CLICK',
                    sound: 'default',
                    badge: '1'
                }
            });
        }

    });
Run Code Online (Sandbox Code Playgroud)

应用程序的 MyData 结构。enter code here

我的应用程序图标

Jig*_*iya 7

对于手动执行此操作的徽章,您必须将用户未读消息计数存储在 firebase 中的某个位置(建议在用户节点中)。因此,当用户读取或获取消息时,您需要更新该计数

这是一个软件包,可帮助您在应用程序图标上显示徽章。flutter_app_badger https://pub.dev/packages/flutter_app_badger

FlutterAppBadger 可以让您更新应用程序图标的计数。它支持 IOS 和 Android 这样。

FlutterAppBadger.updateBadgeCount(1);
Run Code Online (Sandbox Code Playgroud)

移除徽章

FlutterAppBadger.removeBadge();
Run Code Online (Sandbox Code Playgroud)