aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

onNotificationReceivedInForeground from aws-amplify/push-notifications is not triggered #13257

Closed victoravr closed 5 months ago

victoravr commented 5 months ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Push Notifications

Amplify Version

v6

Amplify Categories

notifications

Backend

CDK

Environment information

``` # Put output below this line System: OS: macOS 14.4.1 CPU: (8) arm64 Apple M2 Memory: 124.08 MB / 24.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 21.7.2 - /opt/homebrew/bin/node Yarn: 1.22.22 - /opt/homebrew/bin/yarn npm: 10.5.0 - /opt/homebrew/bin/npm Watchman: 2024.04.08.00 - /opt/homebrew/bin/watchman Browsers: Edge: 123.0.2420.97 Safari: 17.4.1 npmPackages: @aws-amplify/react-native: ^1.0.28 => 1.0.28 @aws-amplify/rtn-push-notification: ^1.2.28 => 1.2.28 @aws-amplify/ui-react-native: ^2.1.5 => 2.1.5 @aws-crypto/sha256-js: ^5.2.0 => 5.2.0 (3.0.0) @babel/core: ^7.23.6 => 7.24.0 @babel/preset-env: ^7.23.6 => 7.24.0 @babel/preset-react: ^7.23.3 => 7.23.3 @babel/runtime: ^7.23.6 => 7.24.0 @react-native-async-storage/async-storage: ^1.23.1 => 1.23.1 @react-native-community/netinfo: ^11.2.1 => 11.3.1 @react-native-community/slider: ^4.5.0 => 4.5.0 @react-native-picker/picker: ^2.6.1 => 2.6.1 @react-native/babel-preset: 0.73.21 => 0.73.21 @react-native/eslint-config: 0.73.2 => 0.73.2 @react-native/metro-config: 0.73.5 => 0.73.5 @react-native/typescript-config: 0.73.1 => 0.73.1 @react-navigation/bottom-tabs: ^6.5.11 => 6.5.16 @react-navigation/drawer: ^6.6.6 => 6.6.11 @react-navigation/native: ^6.1.9 => 6.1.14 @react-navigation/stack: ^6.3.20 => 6.3.25 @reduxjs/toolkit: ^2.0.1 => 2.2.1 @reduxjs/toolkit-query: 1.0.0 @reduxjs/toolkit-query-react: 1.0.0 @reduxjs/toolkit-react: 1.0.0 @rneui/base: ^4.0.0-rc.7 => 4.0.0-rc.7 @rneui/themed: ^4.0.0-rc.8 => 4.0.0-rc.8 @rnx-kit/align-deps: ^2.3.1 => 2.3.4 @tanstack/query-codemods: 4.24.3 @tanstack/react-query: ^5.28.4 => 5.28.4 @tsconfig/react-native: ^3.0.2 => 3.0.3 @types/react: ^18.2.45 => 18.2.61 @types/react-redux: ^7.1.33 => 7.1.33 @types/react-test-renderer: ^18.0.7 => 18.0.7 HelloWorld: 0.0.1 aws-amplify: ^6.0.28 => 6.0.28 aws-amplify/adapter-core: undefined () aws-amplify/analytics: undefined () aws-amplify/analytics/kinesis: undefined () aws-amplify/analytics/kinesis-firehose: undefined () aws-amplify/analytics/personalize: undefined () aws-amplify/analytics/pinpoint: undefined () aws-amplify/api: undefined () aws-amplify/api/server: undefined () aws-amplify/auth: undefined () aws-amplify/auth/cognito: undefined () aws-amplify/auth/cognito/server: undefined () aws-amplify/auth/enable-oauth-listener: undefined () aws-amplify/auth/server: undefined () aws-amplify/data: undefined () aws-amplify/data/server: undefined () aws-amplify/datastore: undefined () aws-amplify/in-app-messaging: undefined () aws-amplify/in-app-messaging/pinpoint: undefined () aws-amplify/push-notifications: undefined () aws-amplify/push-notifications/pinpoint: undefined () aws-amplify/storage: undefined () aws-amplify/storage/s3: undefined () aws-amplify/storage/s3/server: undefined () aws-amplify/storage/server: undefined () aws-amplify/utils: undefined () babel-jest: ^29.7.0 => 29.7.0 babel-loader: ^9.1.3 => 9.1.3 detox: ^20.19.5 => 20.19.5 eslint: ^8.56.0 => 8.57.0 formik: ^2.4.5 => 2.4.5 ini: ^4.1.1 => 4.1.1 (1.3.8) inquirer: ^9.2.12 => 9.2.15 jest: ^29.7.0 => 29.7.0 metro-react-native-babel-preset: ^0.77.0 => 0.77.0 prettier: ^3.1.1 => 3.2.5 react: 18.2.0 => 18.2.0 react-dom: ^18.2.0 => 18.2.0 react-native: ^0.73.6 => 0.73.6 react-native-awesome-slider: ^2.4.6 => 2.5.1 react-native-codegen: ^0.70.7 => 0.70.7 react-native-fast-image: ^8.6.3 => 8.6.3 react-native-gesture-handler: ^2.14.0 => 2.15.0 react-native-get-random-values: ^1.11.0 => 1.11.0 react-native-image-crop-picker: ^0.40.2 => 0.40.3 react-native-modal: ^13.0.1 => 13.0.1 react-native-picker-select: ^9.0.1 => 9.0.1 react-native-purchases: ^7.5.1 => 7.22.0 react-native-reanimated: ^3.6.1 => 3.7.2 react-native-safe-area-context: ^4.8.2 => 4.9.0 react-native-screens: ^3.29.0 => 3.29.0 react-native-toast-message: ^2.2.0 => 2.2.0 react-native-url-polyfill: ^2.0.0 => 2.0.0 react-native-vector-icons: ^10.0.3 => 10.0.3 react-redux: ^9.0.4 => 9.1.0 react-test-renderer: 18.2.0 => 18.2.0 redux-persist: ^6.0.0 => 6.0.0 redux-persist/integration/react: undefined () redux-thunk: ^3.1.0 => 3.1.0 typescript: ^5.3.3 => 5.3.3 yup: ^1.3.3 => 1.3.3 npmGlobalPackages: @devicecloud.dev/dcd: 0.0.5 detox-cli: 20.0.0 ios-deploy: 1.12.2 npm: 10.5.0 ```

Describe the bug

onNotificationReceivedInForeground from aws-amplify/push-notifications is not triggered when app is in Foreground. Background notifications are OK.

Expected behavior

onNotificationReceivedInForeground from aws-amplify/push-notifications is triggered.

Reproduction steps

  1. Send APNs sandbox message
  2. If app is in background, message will be received OK
  3. If app is in foreground, nothing happens

Code Snippet

App.tsx

...
import {
  onNotificationReceivedInForeground,
  OnNotificationReceivedInForegroundInput,
  OnNotificationReceivedInForegroundOutput,
} from 'aws-amplify/push-notifications';
...

function App(): React.JSX.Element { 
...
 useEffect(() => {
    const foregroundNotificationReceivedHandler: OnNotificationReceivedInForegroundInput =
      notification => {
        console.log(
          `Got a new foregroundNotification: ${JSON.stringify(notification)}`,
        );
    }
    const foregroundNotificationListener: OnNotificationReceivedInForegroundOutput =
      onNotificationReceivedInForeground(foregroundNotificationReceivedHandler);
...
    return () => {
      foregroundNotificationListener.remove();
    };
  }, []);

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

iPhone 15 Pro Simulator

Mobile Operating System

iOS 17.4

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

cwomack commented 5 months ago

Hello, @victoravr 👋. Hoping to get some clarity around the behavior that you're reporting in this issue to make sure it's not expected. If the app is in the foreground state, then notifications are not displayed when received (see here). But are you saying that the onNotificationReceivedInForeground() is not being triggered at all on iOS? Is this happening when the app is being booted up for the first time (or any time after being completely killed/shut down)?

cwomack commented 5 months ago

@victoravr, we're able to reproduce this on our side and are investigating the root cause further. Labeling this as a bug for now.

cshfang commented 5 months ago

Hi @victoravr - I am trying to look into this. Can I ask if you are seeing this behavior on actual iOS device as well? When it comes to push notifications, we are still seeing that simulators do not handle these use cases correctly.

victoravr commented 5 months ago

Hi @victoravr - I am trying to look into this. Can I ask if you are seeing this behavior on actual iOS device as well? When it comes to push notifications, we are still seeing that simulators do not handle these use cases correctly.

Yes, I've seen this behavior on the actual iOS device as well during my testing.

cshfang commented 5 months ago

Hi @victoravr, I am still working on a way to consistently reproduce this on an actual device. I can definitely confirm that, I am seeing this behavior on simulator, but they seem to resolve on an actual iOS device. Here is a log of my Xcode logs

image

On simulator, I find that using the simctl command has been effective in simulating a foreground PN:

// apns.json
{
  "aps" : {
    "alert" : {
      "title" : "Title",
      "body" : "Body"
    }
  }
}
$ xcrun simctl push booted <bundle-identifier> ./apns.json

When testing on real devices, I have definitely seen instances where console logging stops working, could you please make sure this isn't what's happening? It could be helpful if you can provide some full logs from your iOS device with some of the other Amplify Push Notification APIs exercise as well - so we can try and trace down any patterns.

E.g. on a bare RN app:

useEffect(() => {
  console.log('adding listeners');
  const backgroundNotificationListener = onNotificationReceivedInBackground(
    notification => {
      console.log(
        `Got a new backgroundNotification: ${JSON.stringify(notification)}`,
      );
    },
  );
  const foregroundNotificationListener = onNotificationReceivedInForeground(
    notification => {
      console.log(
        `Got a new foregroundNotification: ${JSON.stringify(notification)}`,
      );
    },
  );
  const notificationOpenedListener = onNotificationOpened(notification => {
    console.log(`Opened a notification: ${JSON.stringify(notification)}`);
  });
  const tokenReceivedListener = onTokenReceived(token => {
    console.log(`Got a new token: ${token}`);
  });

  return () => {
    backgroundNotificationListener.remove();
    foregroundNotificationListener.remove();
    notificationOpenedListener.remove();
    tokenReceivedListener.remove();
  };
}, []);
victoravr commented 5 months ago

Hi @victoravr, I am still working on a way to consistently reproduce this on an actual device. I can definitely confirm that, I am seeing this behavior on simulator, but they seem to resolve on an actual iOS device.

Hi @cshfang, Yes, you are right. Just tested APNS push notification on the physical iOS device and it's working. Is there a way to fix it for iOS simulator? Thanks!

cshfang commented 5 months ago

Hey @victoravr. It's good to hear that it's working for you! As mentioned, you can use simctl locally to test the functionality on simulator but, unfortunately, Push Notifications on iOS simulator is still not quite at parity with the real thing. This is a limitation that Apple has been making strides in recent OS updates to overcome but is still not quite there.

Given that you are able to verify now that this is working on a physical device, can we consider this issue resolved?

cwomack commented 5 months ago

@victoravr, appreciate your confirmations and follow ups here after @cshfang was able to root cause this. I'll close this issue out and remove the bug label since there's no issue on the Amplify side of things.