cordova-plugin-badge icon indicating copy to clipboard operation
cordova-plugin-badge copied to clipboard

How to increment badge when app not in foreground

Open timstoute opened this issue 4 years ago • 1 comments

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 avatar Apr 20 '21 22:04 timstoute

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

timstoute avatar Apr 21 '21 22:04 timstoute