简体   繁体   中英

Flutter/Firebase How to increment Badge number of App Icon

I am student who is studying computer programming in Canada. Nowadays , I am making the app as my side project with flutter/firebase.

In my app chat function is included. The problem that I am facing is how to increment badge number and keep it if the message is not read.

I attached the picture of my Message data structure in the firebase.

and below code is the firebase function code. For now I am pushing the notification with this code. I am getting the notification successfully , but the problem is the how to count badge number of the app icon. As you can see in the notification badge number is '1'. Whenever I push the notification, that number is overriding previous number. I think this is wrong way, what I am expecting, if the value isMessageRead in the firebase database increment badge number. I am just wondering the logical way , or any other source that I can study that can explain how to count the Icon badge Number.

For now I have no idea how to manage badge number.

Thanks for reading,

I am waiting for your reply.

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

我的应用程序图标

For badge to do that manually you have to store user unread message count somewhere in firebase (recommend in user node.)So when user read or get messages then you need to update that count number

Here is a package that will help you show a badge on app icons. flutter_app_badger https://pub.dev/packages/flutter_app_badger

FlutterAppBadger that can you update your count on app icons. It supports Both IOS and Android like this.

FlutterAppBadger.updateBadgeCount(1);

Remove a badge

FlutterAppBadger.removeBadge();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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