Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[notifications] badge does not "reset" when calling setBadgeCountAsync(0) on Android #29634

Closed
haileyok opened this issue Jun 11, 2024 · 1 comment · Fixed by #29657
Closed
Labels
needs review Issue is ready to be reviewed by a maintainer

Comments

@haileyok
Copy link
Contributor

haileyok commented Jun 11, 2024

Minimal reproducible example

https://github.com/haileyok/expo-notifications-badge-repro

What platform(s) does this occur on?

Android - Samsung models, have not tested on others yet

Did you reproduce this issue in a development build?

Yes

Summary

On Android - at least on some devices, tested on Samsung - calling setBadgeCountAsync(0) does not reset the badge. Calling setBadgeCountAsync() and supplying any other positive number updates the badge correctly, but resetting it does not.

I tried using both applyCount and resetCount from ShortcutBadger but neither worked - even though they both return true (or simply does not throw when using the applyCountOrThrow that this package currently uses).

Screen.Recording.2024-06-10.at.9.49.48.PM.mov

I managed to get this to work with a little patch, although I don't know if it's a viable solution. I'm fine with PRing if it is, though I don't know what other consequences it might come with.

diff --git a/node_modules/expo-notifications/android/src/main/java/expo/modules/notifications/badge/BadgeHelper.kt b/node_modules/expo-notifications/android/src/main/java/expo/modules/notifications/badge/BadgeHelper.kt
index 63a46c5..22322f3 100644
--- a/node_modules/expo-notifications/android/src/main/java/expo/modules/notifications/badge/BadgeHelper.kt
+++ b/node_modules/expo-notifications/android/src/main/java/expo/modules/notifications/badge/BadgeHelper.kt
@@ -12,7 +12,12 @@ object BadgeHelper {
 
   fun setBadgeCount(context: Context, badgeCount: Int): Boolean {
     return try {
-      ShortcutBadger.applyCountOrThrow(context.applicationContext, badgeCount)
+      if (badgeCount == 0) {
+        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager
+        notificationManager.cancelAll()
+      } else {
+        ShortcutBadger.applyCountOrThrow(context.applicationContext, badgeCount)
+      }
       BadgeHelper.badgeCount = badgeCount
       true
     } catch (e: ShortcutBadgeException) {
Screen.Recording.2024-06-10.at.9.51.04.PM.mov

Repro Steps

  • Checkout the repro and prebuild
  • Run on Android
  • Request permissions, and schedule a notification. As soon as you press the schedule button, minimize the app.
  • After two seconds the notification should have badged the app. Return to the app and press the clear badge button.
  • Note that the badge is not cleared.

To see the patch working, remove .disabled from the patch in ./patches and do the same, except note that after pressing the clear badge button, the badge does in fact reset.

Environment

expo-env-info 1.2.0 environment info:
System:
OS: macOS 14.5
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.19.1 - ~/.nvm/versions/node/v18.19.1/bin/node
Yarn: 1.22.21 - ~/.nvm/versions/node/v18.19.1/bin/yarn
npm: 10.4.0 - ~/.nvm/versions/node/v18.19.1/bin/npm
Watchman: 2024.05.06.00 - /opt/homebrew/bin/watchman
Managers:
CocoaPods: 1.15.2 - /Users/hailey/.rbenv/shims/pod
SDKs:
iOS SDK:
Platforms: DriverKit 23.5, iOS 17.5, macOS 14.5, tvOS 17.5, visionOS 1.2, watchOS 10.5
IDEs:
Android Studio: 2023.1 AI-231.9392.1.2311.11330709
Xcode: 15.4/15F31d - /usr/bin/xcodebuild
npmPackages:
expo: ~51.0.12 => 51.0.12
expo-router: ~3.5.16 => 3.5.16
react: 18.2.0 => 18.2.0
react-dom: 18.2.0 => 18.2.0
react-native: 0.74.2 => 0.74.2
react-native-web: ~0.19.10 => 0.19.12
npmGlobalPackages:
eas-cli: 7.5.0
Expo Workflow: bare

Expo Doctor Diagnostics

✔ Check Expo config for common issues
✔ Check package.json for common issues
✔ Check dependencies for packages that should not be installed directly
✔ Check for issues with metro config
✔ Check for common project setup issues
✔ Check npm/ yarn versions
✔ Check Expo config (app.json/ app.config.js) schema
✔ Check native tooling versions
✔ Check for legacy global CLI installed locally
✔ Check that native modules do not use incompatible support packages
✔ Check that packages match versions required by installed Expo SDK
✔ Check that native modules use compatible support package versions for installed Expo SDK

Didn't find any issues with the project!

@haileyok haileyok added the needs validation Issue needs to be validated label Jun 11, 2024
@expo-bot expo-bot added needs review Issue is ready to be reviewed by a maintainer and removed needs validation Issue needs to be validated labels Jun 11, 2024
@iM-GeeKy
Copy link

iM-GeeKy commented Jun 11, 2024

@haileyok Perhaps you might find this helpful or have already seen it, but this is related to #13311 (comment). I had the same issue and had to apply a workaround similar to the comment.

douglowder added a commit that referenced this issue Jun 11, 2024
… 0 (#29657)

# Why

The `ShortcutBadger` package does not correctly set the badge count on
Android if the value to be set is 0. This fixes the issue, using a fix
provided by @haileyok in #29634 .

Fixes #29634.

# How

In the case where badge count is 0, use Android built in notification
manager to cancel all notifications.

# Test Plan

- CI should pass
- API should behave correctly in our test app

# Checklist

<!--
Please check the appropriate items below if they apply to your diff.
This is required for changes to Expo modules.
-->

- [ ] Documentation is up to date to reflect these changes (eg:
https://docs.expo.dev and README.md).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).
douglowder added a commit that referenced this issue Jun 11, 2024
… 0 (#29657)

# Why

The `ShortcutBadger` package does not correctly set the badge count on
Android if the value to be set is 0. This fixes the issue, using a fix
provided by @haileyok in #29634 .

Fixes #29634.

# How

In the case where badge count is 0, use Android built in notification
manager to cancel all notifications.

# Test Plan

- CI should pass
- API should behave correctly in our test app

# Checklist

<!--
Please check the appropriate items below if they apply to your diff.
This is required for changes to Expo modules.
-->

- [ ] Documentation is up to date to reflect these changes (eg:
https://docs.expo.dev and README.md).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).
marklawlor pushed a commit that referenced this issue Jun 12, 2024
… 0 (#29657)

# Why

The `ShortcutBadger` package does not correctly set the badge count on
Android if the value to be set is 0. This fixes the issue, using a fix
provided by @haileyok in #29634 .

Fixes #29634.

# How

In the case where badge count is 0, use Android built in notification
manager to cancel all notifications.

# Test Plan

- CI should pass
- API should behave correctly in our test app

# Checklist

<!--
Please check the appropriate items below if they apply to your diff.
This is required for changes to Expo modules.
-->

- [ ] Documentation is up to date to reflect these changes (eg:
https://docs.expo.dev and README.md).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).
douglowder added a commit that referenced this issue Jun 12, 2024
… 0 (#29657)

# Why

The `ShortcutBadger` package does not correctly set the badge count on
Android if the value to be set is 0. This fixes the issue, using a fix
provided by @haileyok in #29634 .

Fixes #29634.

# How

In the case where badge count is 0, use Android built in notification
manager to cancel all notifications.

# Test Plan

- CI should pass
- API should behave correctly in our test app

# Checklist

<!--
Please check the appropriate items below if they apply to your diff.
This is required for changes to Expo modules.
-->

- [ ] Documentation is up to date to reflect these changes (eg:
https://docs.expo.dev and README.md).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs review Issue is ready to be reviewed by a maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants