invertase / notifee

⚛️ A feature rich notifications library for React Native.
https://notifee.app
Apache License 2.0
1.76k stars 201 forks source link

Android 12 - Clicking notification not bringing background app to foreground #503

Closed egadstar closed 1 year ago

egadstar commented 1 year ago

I've looked through the existing issues similar to what I'm experiencing, and nothing has seemed to resolved the issue. The message from FCM looks correct based on the documentation; I'm sending it via the Java SDK. The notifee onBackgroundEvent is triggering on the receive of the message and when the notification is cleared from the tray, but doesn't receive an event on click of the notification from the tray and the app isn't being restored to the foreground. I've also tried targeting SDK 30 and running on an emulator with Android 11; I feel like something has to be configured incorrectly.

"@notifee/react-native": "5.6.0" "@react-native-firebase/app": "15.3.0" "@react-native-firebase/messaging": "15.3.0"

System:

    OS: macOS 12.5.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 24.83 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.17.0 - ~/.nvm/versions/node/v16.17.0/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 8.15.0 - ~/.nvm/versions/node/v16.17.0/bin/npm
    Watchman: 2022.08.15.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.11.2 - /Users/pwashburn/.rvm/gems/ruby-2.7.5/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 15.5, macOS 12.3, tvOS 15.4, watchOS 8.5
    Android SDK: Not Found
  IDEs:
    Android Studio: 2021.2 AI-212.5712.43.2112.8609683
    Xcode: 13.4.1/13F100 - /usr/bin/xcodebuild
  Languages:
    Java: 17.0.3.1 - /Library/Java/JavaVirtualMachines/jdk-17.0.3.1.jdk/Contents/Home/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 18.2.0 => 18.2.0 
    react-native: 0.69.4 => 0.69.4 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

index.js

// setup notifee and FCM
const setupAndroidChannel = async () => {
  await notifee.createChannel({
    id: 'one',
    name: 'One',
    description: 'Default communication channel for the platform',
    lights: true,
    vibration: true,
    badge: true,
    importance: AndroidImportance.HIGH,
  });
};

notifee.onBackgroundEvent(async ({ type, detail }) => {
  const { notification } = detail;
  console.log("background event: ", type, detail)
  if (type === EventType.PRESS) {
    console.log(type, detail)
    await notifee.cancelNotification(notification.id);
  }
});

const onMessageReceived = async (message) => {
  console.log("message: ", message)
  console.log("notifee: ", JSON.parse(message.data.notifee));
  await notifee.displayNotification(SON.parse(message.data.notifee))
};

setupAndroidChannel();
messaging().onMessage(onMessageReceived);
messaging().setBackgroundMessageHandler(onMessageReceived);

const Main = () => {}

AppRegistry.registerComponent(appName, () => Main);

build.gradle

ext {
        googlePlayServicesVersion = "16.1.0"
        googlePlayServicesIidVersion = "16.0.1"
        firebaseVersion = "21.1.0"
        googleServices = "4.3.3"
        buildToolsVersion = "30.0.2"
        minSdkVersion = 29
        compileSdkVersion = 31
        targetSdkVersion = 31
        kotlinVersion = "1.5.0"

        if (System.properties['os.arch'] == "aarch64") {
            // For M1 Users we need to use the NDK 24 which added support for aarch64
            ndkVersion = "24.0.8215888"
        } else if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            // For Android Users, we need to use NDK 23, otherwise the build will
            // fail due to paths longer than the OS limit
            ndkVersion = "23.1.7779620"
        } else {
            // Otherwise we default to the side-by-side NDK version from AGP.
            ndkVersion = "21.4.7075529"
        }
    }

message.data.notifee received from messaging.setBackgroundMessageHandler

{
  "android": {
    "autoCancel": false,
    "channelId": "one",
    "color": "#E65400",
    "importance": 4,
    "pushAction": {
      "id": "default",
      "launchActivity": "default"
    },
    "smallIcon": "notification_icon"
  },
  "body": "Testing notifee setup.",
  "data": {
    "id": "127889",
    "product": "one"
  }
}

logs

08-25 09:49:49.233  4935  5145 I ReactNativeJS: 'background event: ', 3, { notification: 
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:    { data: 
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:       { id: '127889',
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         product: 'one',
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:      id: 'Wmi3IQa0jYFxjbzhIF70',
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:      body: 'Testing notifee setup.',
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:      android: 
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:       { importance: 4,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         groupSummary: false,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         colorized: false,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         lightUpScreen: false,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         loopSound: false,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         visibility: 0,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         circularLargeIcon: false,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         smallIcon: 'notification_icon',
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         color: '#E65400',
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         asForegroundService: false,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         ongoing: false,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         showTimestamp: false,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         badgeIconType: 2,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         onlyAlertOnce: false,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         groupAlertBehavior: 0,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         showChronometer: false,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         channelId: 'one',
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         autoCancel: false,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         localOnly: false,
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         defaults: [ -1 ],
  08-25 09:49:49.233  4935  5145 I ReactNativeJS:         chronometerDirection: 'up' } } }
  08-25 09:49:49.237   715   715 W TouchableRegionManager: onHeadsUpPinnedModeChanged
  08-25 09:49:49.245   715   715 D StatusBar: disable<e i a s b h r c s > disable2<q i n >
  08-25 09:49:53.826   715   715 D ActivityLaunchAnimator: Starting intent with a launch animation
  08-25 09:49:53.828   548  2007 I ActivityTaskManager: START u0 {flg=0x18080000 cmp=one/app.notifee.core.NotificationReceiverActivity (has extras)} from uid 10150
  08-25 09:49:53.830   548  2007 W ActivityTaskManager: Can't find TaskDisplayArea to determine support for multi window. Task id=35 attached=false
  08-25 09:49:53.833   373   510 D AudioFlinger: mixer(0x7d33dc529a70) throttle end: throttle time(36)
  08-25 09:49:53.838   715   715 D ActivityLaunchAnimator: launchResult=0 willAnimate=true isOnKeyguard=false
  08-25 09:49:53.843   548  3740 W ActivityTaskManager: Tried to set launchTime (0) < mLastActivityLaunchTime (50865926)
  08-25 09:49:53.876   715  1157 D ActivityLaunchAnimator: Remote animation was cancelled
helenaford commented 1 year ago

if the notifee onBackgroundEvent is triggered, does that mean the notification was first delivered when the app was in the foreground?

egadstar commented 1 year ago

it was running in the background. i figure this is an indicator that i don't have something configured correctly.

David-Montenegro commented 1 year ago

use const initialNotification = await notifee.getInitialNotification(); for Android in your home screen to trigger

egadstar commented 1 year ago

I'm going to close this issue as it is currently working after adjusting my setup to rely Firebase more and using notifee for covering just the foreground presentation. I'm sure I didn't have the message 100% correct or something else configured correctly. I did notice that I had removed the launchMode from the AndroidManifest at some point; not sure if that would play into it. Thanks for taking the time to look at the issue.