katzer / cordova-plugin-badge

Access and modify badge numbers
Apache License 2.0
408 stars 253 forks source link

How to increment badge when app not in foreground #146

Open timstoute opened 3 years ago

timstoute commented 3 years ago

I'm trying to set the badge count for the user when the app is running in the background. But it's not working.

I'm using Firestore, and save the user-specific number of new messages the user has as a field on the Firestore user document. This is an Ionic Cordova app.

The following code works when the app is in the foreground, but not when the app is running in the background on the IOS device.

In app.component.ts I have code like:

` if ((this.platform.is('ios')) || (this.platform.is('android'))) { this.appBadge.requestPermission() .then(async result => {

          if (this.appBadge.hasPermission()) {

            // get appBadge count for user 
            await this.auth.user$?.subscribe(user => {
              this.user = user;

              this.common.getUserChatNotifications(user).subscribe(
                chatNotificationCount  => {
                 // set the badge value
                 this.appBadge.set(chatNotificationCount)
                  })
        })

...` What am I missing?

timstoute commented 3 years ago

it looks like Firebase Cloud Messaging supports setting badge values, and that looks like the best way to achieve both notifications and badges when the app is not running or not in the foreground:https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support