Expo Notifications addNotificationReceivedListener not being called in foreground only on Android standalone

660 Views Asked by At

I am trying to receive notifications in my Expo app. I can successfully register a token, and the notification is received fine when the app is not foregrounded. However, on Android only the addNotificationReceivedListener callback is not being triggered. This works fine on iOS when the app is foregrounded and Android, only when the app is not foregrounded. Here is my code for registering the callback.

React.useEffect(() => {
      Notifications.setNotificationHandler({
         handleNotification: async () => ({
            shouldShowAlert: true,
            shouldPlaySound: true,
            shouldSetBadge: false,
         }),
      });
      Notifications.addNotificationReceivedListener(() => {
         dispatch(setBalance({ amount: null }));
      });
   }, []);

And here is my app.json

{
   "name": "AppName",
   "displayName": "AppName",
   "expo": {
      "name": "AppName",
      "icon": "./assets/icon.png",
      "slug": "slug",
      "version": "1.0.0",
      "assetBundlePatterns": ["**/*"],
      "scheme": "appscheme",
      "android": {
         "package": "com.appdomain",
         "enableDangerousExperimentalLeanBuilds": true,
         "versionCode": 7,
         "googleServicesFile": "./google-services.json",
         "useNextNotificationsApi": true,
         "permissions": ["RECEIVE_BOOT_COMPLETED"]
      },
      "ios": {
         "bundleIdentifier": "com.appdomain",
         "buildNumber": "7"
      }
   }
}

I am using Expo 41 which I just upgraded to from 40. It was not working on 40 either. Thanks.

1

There are 1 best solutions below

0
On

You may have android.useNextNotificationsApi set to true in app.json.