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.41k stars 2.12k forks source link

AWS amplify notifier not triggered #13585

Open jamespanyp64 opened 1 month ago

jamespanyp64 commented 1 month ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Push Notifications

Amplify Version

v6

Amplify Categories

notifications

Backend

Amplify CLI

Environment information

``` System: OS: macOS 14.3.1 CPU: (11) arm64 Apple M3 Pro Memory: 71.11 MB / 18.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.11.0 - /usr/local/bin/node npm: 10.2.4 - /usr/local/bin/npm Browsers: Chrome: 126.0.6478.127 Safari: 17.3.1 npmPackages: @aws-amplify/react-native: ^1.1.1 => 1.1.1 @aws-amplify/rtn-push-notification: ^1.2.29 => 1.2.29 @babel/core: ^7.20.0 => 7.24.7 @babel/preset-env: ^7.20.0 => 7.24.7 @babel/runtime: ^7.20.0 => 7.24.7 @react-native-async-storage/async-storage: ^1.23.1 => 1.23.1 @react-native-community/netinfo: ^11.3.2 => 11.3.2 @react-native/babel-preset: 0.74.85 => 0.74.85 @react-native/eslint-config: 0.74.85 => 0.74.85 @react-native/metro-config: 0.74.85 => 0.74.85 @react-native/typescript-config: 0.74.85 => 0.74.85 @types/react: ^18.2.6 => 18.3.3 @types/react-test-renderer: ^18.0.0 => 18.3.0 HelloWorld: 0.0.1 aws-amplify: ^6.3.8 => 6.3.8 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.6.3 => 29.7.0 eslint: ^8.19.0 => 8.57.0 jest: ^29.6.3 => 29.7.0 prettier: 2.8.8 => 2.8.8 react: 18.2.0 => 18.2.0 react-native: 0.74.3 => 0.74.3 react-native-get-random-values: ^1.11.0 => 1.11.0 react-redux: ^9.1.2 => 9.1.2 react-test-renderer: 18.2.0 => 18.2.0 redux: ^5.0.1 => 5.0.1 typescript: 5.0.4 => 5.0.4 npmGlobalPackages: @aws-amplify/cli: 12.10.1 react-native-cli: 2.0.1 ```

Describe the bug

AWS backgroundNotificationListener not triggered at all.

Expected behavior

AWS backgroundNotificationListener should be triggered when the app is on the background or terminated.

Reproduction steps

  1. Followed https://docs.amplify.aws/gen1/react-native/build-a-backend/push-notifications/set-up-push-notifications/#initialize-amplify-push-notifications to do the setup and then start the simulator.
  2. Used AWS test messaging to send message against the device token using APNs sandbox.
  3. BackgroundNotificaitonListener not triggered at all.

Code Snippet

index.js

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';

import {Amplify} from 'aws-amplify';
import {initializePushNotifications} from 'aws-amplify/push-notifications';
import amplifyconfig from './src/amplifyconfiguration.json';

import {backgroundNotificationListener} from './listeners/notificationListener';

Amplify.configure(amplifyconfig);
initializePushNotifications();

let _ = backgroundNotificationListener();

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

notificationListener.js

import {
  getBadgeCount,
  setBadgeCount,
  onNotificationReceivedInForeground,
  onNotificationReceivedInBackground,
  onNotificationOpened
} from 'aws-amplify/push-notifications';

const notificationReceivedHandler = async notification => {
  console.log('notificationReceivedHandler', notification);

  let current_notification_count = await getBadgeCount();
  await setBadgeCount(current_notification_count + 1);
};

export const foregroundNotificationListener = () => {
  return onNotificationReceivedInForeground(notificationReceivedHandler);
};

export const backgroundNotificationListener = () => {
  onNotificationReceivedInBackground(notificationReceivedHandler);
};

const notificationOpenedHandler = notification => {
  console.log('notificationOpenedHandler', notification);
};

export const onNotificationOpenedListener = () => {
  return onNotificationOpened(notificationOpenedHandler);
};

Log output

``` Running "amplified_todo" with {"rootTag":1,"initialProps":{"concurrentRoot":false}} LOG tokenListener LOG tokenReceivedHandler 80f384d82ca20f05560651295e39bd45109044383e57fbd929896e49329c93e5d69c59d8b92a5e18577ec76e75f59d441cd6a4f76b55844f5712df240e0f884c048e30d1e90478c8065d85a535de2d91 LOG Permission status: shouldExplainThenRequest LOG tokenListener ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

cwomack commented 1 month ago

Hello, @jamespanyp64 👋. Have a few questions to ask you tied to this to see if we can better diagnose the root cause:

Cosmooooo commented 1 month ago

Hi, thank you for getting back quickly.

  1. We did followed the entitlements page for setup, and in our Xcode, it do show we have remote notification and push notification enabled. The only step we did perform is step 4 and step 6 as we didn't see change All is prompted. amplify
  2. We have tested this through physical device, as we know, ios simulator isn't working consistently with listeners.
  3. The problem is happening consistently.
  4. We are only working with iOS at this point.
ashwinkumar6 commented 1 month ago

Hi @Cosmooooo, We're investigating this issue, in the mean time could you try increasing the priority to High and see if this helps

jamespanyp64 commented 1 month ago

Hi @ashwinkumar6 - Thanks for getting back to us but unfortunately increasing to high priority didn't work for us either. Wondering if there's any update or ETA of fix on the issue?

Samaritan1011001 commented 1 month ago

Hi @jamespanyp64, thanks for checking that. On our side, we tested with RN 0.73 and 0.74 and don't see this issue happening. However, we did find that when testing back to back background messages that the iOS system can throttle the message delivery behavior (which is something we can't control). Please refer this piece of documentation which explains why they do a best effort to deliver it.

In my efforts to improve it, I found that updating the apns-push-type header value to background might increase it being delivered to the device. This header is to be set by Pinpoint when pushing the message to APNs but you may test it using Apple's Push Notification console directly. You can find more information of the header value here. Note that this is best efforts to improve the delivery from APNs to the device when testing.

Cosmooooo commented 1 month ago

Hi @Samaritan1011001, Thank you for this info. Unfortunately, we have tried to increase priority and using background type, but background listener is still not able to be triggered consistently. At the same time, we don't think background is what we are trying to use, what we are trying to do is have our custom logic (for example, badge count increase) be executed when a normal notification (alert) comes in when app is in background or terminated state. We would like to confirm that we should use Amplify background listener for this purpose and there is no misunderstanding during conversation.

Samaritan1011001 commented 1 month ago

@Cosmooooo Thank you for the use-case info. To increment or set the badge count this way, I would suggest you to use the backend notification payload since iOS does not reliably and consistently invoke the native APIs that onNotificationReceivedInBackground uses. iOS restrictions as stated here:

However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

You may specify the badge count when sending the notification from Pinpoint. When you use the "raw message" on the Pinpoint console or their API, you can provide the badge count in the payload as shown below.

"APNSMessage": {
  "aps": {
      "alert": {
        "title" : "title",
        "body"  : "body"
      },
      "badge": 100
  }
},
jamespanyp64 commented 3 weeks ago

Hi @Samaritan1011001, thanks for your reply! As @Cosmooooo mentioned above, we are trying to have some custom logic executed in onNotificationReceivedInBackground. The basic reason is this: we are implementing a chat function in our app, and thus there's a screen for showing "all the chat rooms I'm in". Currently we need to do a GET against all the rooms to get the latest message which has a bad performance because there are multiple rooms that don't have any new messages at all. Thus we'd like to rely on this amplify background listener as a tool to help us update the rooms with new messages when the app is in background, so that we don't have to do the GET again when the user opens the app. Based on the context above, can you please provide some suggestions on what amplify tool we can use to improve the performance?