Sanjay Tibrewal
Sanjay Tibrewal

Reputation: 143

Unable to clear FCM notification badge count on Android in Ioinic Angular App

I am working on an Ionic Angular app using the Firebase FCM for sending push notifications and am using the capacitor badge to clear the count once the user enters/leaves the App. Inside the AndroidNotification interface (which is inside the AndroidConfig interface) is the property notificationCount, for setting the badge count for the App. When I use the clear method on the capacitor badge plugin, it doesn't clear the push notification count sent through the FCM AndroidNotification interface. However if I set a count through capacitor badge plugin and clear then that works. Specifically here are some of the steps to help clarify -

  1. Send say two push notifications and the App badge count increments to 2
  2. Open the App and trigger the Badge.clear but the badge count of 2 remains unchanged
  3. Open the App again and trigger a Badge.set command to a number say 20
  4. Leave the App and the App badge count shows 20
  5. Now open the App and trigger to clear the badge count through Badge.clear and leave the App
  6. App badge count goes back to 2, the original push notification count, while clearing the 20 that was set through Badge.set

I am trying to understand and clear the badge count set through notificationCount on Android devices. The corresponding set and clear behavior works fine on iOS devices where it clears the push notification count set FCM.

I am not sure but I suspect the Android issue may have something to do with channels in Android. The documentation for AndroidNotification interface includes this for channel id

/**
 * The Android notification channel ID (new in Android O). The app must create
 * a channel with this channel ID before any notification with this channel ID
 * can be received. If you don't send this channel ID in the request, or if the
 * channel ID provided has not yet been created by the app, FCM uses the channel
 * ID specified in the app manifest.
 */
channelId?: string;

There is no channel being created or set so the default behavior seems to be "FCM uses the channel ID specified in the app manifest". I am not sure what channel id this is referring to as I don't have anything set in my AndroidManifest.xml file.

My ionic info output is included below for my environment.

Ionic:

   Ionic CLI                     : 7.1.1 (/Users/<username>/.nvm/versions/node/v20.2.0/lib/node_modules/@ionic/cli)
   Ionic Framework               : @ionic/angular 6.7.5
   @angular-devkit/build-angular : 16.2.12
   @angular-devkit/schematics    : 16.2.12
   @angular/cli                  : 16.2.12
   @ionic/angular-toolkit        : 6.1.0

Capacitor:

   Capacitor CLI      : 5.7.0
   @capacitor/android : 5.7.0
   @capacitor/core    : 5.7.0
   @capacitor/ios     : 5.7.0

Cordova:

   Cordova CLI       : 12.0.0 (cordova-lib@12.0.1)
   Cordova Platforms : android 9.1.0, browser 6.0.0, ios 6.2.0
   Cordova Plugins   : cordova-plugin-ionic-webview 4.2.1, (and 3 other plugins)

Utility:

   cordova-res : 0.15.4
   native-run  : 2.0.1

System:

   ios-sim : 8.0.2
   NodeJS  : v20.2.0 (/Users/<username>/.nvm/versions/node/v20.2.0/bin/node)
   npm     : 9.6.6
   OS      : macOS Unknown
   Xcode   : Xcode 15.4 Build version 15F31d

Looking for help in how to debug the issue. I suspect the push notification uses a different channel for setting badge count while the capacitor badge uses some other channel to set and clear badge counts for Android but I am not sure exactly how that works.

I have posted this issue on the capacitor-plugins project and the corresponding shortCutBadger project but haven't received any input. Hoping to get some pointers/help here particularly with respect to possible implication of channels in Android.

Thanks, Sanjay.

Upvotes: 1

Views: 153

Answers (0)

Related Questions