繁体   English   中英

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

[英]Flutter/Firebase How to increment Badge number of App Icon

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

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

我在 firebase 中附上了我的 Message 数据结构的图片。

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

现在我不知道如何管理徽章编号。

谢谢阅读,

我在等你的回复。

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'
                }
            });
        }

    });

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

我的应用程序图标

要让徽章手动执行此操作,您必须将用户未读消息计数存储在 firebase 中的某处(推荐在用户节点中)。因此,当用户阅读或获取消息时,您需要更新该计数

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

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

FlutterAppBadger.updateBadgeCount(1);

移除徽章

FlutterAppBadger.removeBadge();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM