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

Push notifications are repeating infinitely #8949

Closed Parkseokje closed 1 year ago

Parkseokje commented 3 years ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Push Notifications

Amplify Categories

notifications

Environment information

``` System: OS: macOS 11.5.2 CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz Memory: 1.15 GB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 16.5.0 - /usr/local/bin/node Yarn: 1.22.11 - /usr/local/bin/yarn npm: 7.19.1 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman Browsers: Brave Browser: 85.1.14.84 Chrome: 93.0.4577.82 Firefox: 92.0 Safari: 14.1.2 npmPackages: @aws-amplify/pushnotification: ^4.1.1 => 4.1.3 @aws-amplify/ui-components: ^1.5.0 => 1.6.1 @babel/cli: ^7.14.5 => 7.14.5 @babel/core: ^7.14.6 => 7.14.6 @babel/preset-env: ^7.14.5 => 7.14.7 @babel/preset-typescript: ^7.14.5 => 7.14.5 @babel/runtime: ^7.14.6 => 7.14.6 (7.15.4) @bam.tech/react-native-make: ^3.0.0 => 3.0.3 @react-native-async-storage/async-storage: ^1.15.5 => 1.15.5 @react-native-community/cameraroll: ^4.0.4 => 4.0.4 @react-native-community/clipboard: ^1.5.1 => 1.5.1 @react-native-community/eslint-config: ^3.0.0 => 3.0.0 @react-native-community/masked-view: ^0.1.10 => 0.1.11 @react-native-community/netinfo: ^6.0.0 => 6.0.0 @react-native-community/push-notification-ios: ^1.8.0 => 1.8.0 (1.0.3) @react-native-firebase/analytics: ^12.1.0 => 12.1.0 @react-native-firebase/app: ^12.1.0 => 12.1.0 @react-navigation/bottom-tabs: ^5.11.8 => 5.11.11 @react-navigation/core: ^5.15.3 => 5.15.3 (3.7.9) @react-navigation/material-bottom-tabs: ^5.3.14 => 5.3.15 @react-navigation/native: ^5.9.4 => 5.9.4 (3.8.4) @react-navigation/stack: ^5.14.3 => 5.14.5 @testing-library/react-hooks: ^7.0.2 => 7.0.2 @types/jest: ^26.0.23 => 26.0.24 @types/qs: ^6.9.6 => 6.9.7 @types/react: ^17.0.11 => 17.0.14 (16.14.11) @types/react-native: ^0.64.10 => 0.64.11 @types/react-native-calendars: ^1.505.3 => 1.505.4 @types/react-native-global-props: ^1.1.0 => 1.1.0 @types/react-native-keyboard-spacer: ^0.4.1 => 0.4.2 @types/react-native-version-check: ^3.4.1 => 3.4.2 @types/react-test-renderer: ^17.0.1 => 17.0.1 @types/styled-components: ^5.1.10 => 5.1.11 @types/styled-components-react-native: ^5.1.1 => 5.1.1 @types/uuid: ^8.3.0 => 8.3.1 @typescript-eslint/eslint-plugin: ^4.27.0 => 4.28.2 @typescript-eslint/parser: ^4.27.0 => 4.28.2 (3.10.1) HelloWorld: 0.0.1 amazon-cognito-identity-js: ^5.0.3 => 5.0.4 amplify-ui-components-loader: undefined () aws-amplify: ^4.1.1 => 4.1.3 aws-sdk: ^2.931.0 => 2.942.0 axios: ^0.21.1 => 0.21.1 babel-jest: ^27.0.2 => 27.0.6 babel-plugin-root-import: ^6.6.0 => 6.6.0 crypto: ^1.0.1 => 1.0.1 crypto-secure-random-digit: ^1.0.9 => 1.0.9 eslint: ^7.28.0 => 7.30.0 eslint-plugin-import: ^2.23.4 => 2.23.4 eslint-plugin-prettier: ^3.3.1 => 3.4.0 (3.1.2) graphql: ^15.5.0 => 15.5.1 (14.0.0, 14.7.0) graphql-tag: ^2.12.4 => 2.12.5 graphql-ttl-transformer: ^1.1.0 => 1.1.0 hermes-inspector-msggen: 1.0.0 husky: 6.0.0 => 6.0.0 jest: ^27.0.4 => 27.0.6 jest-mock-axios: ^4.4.0 => 4.4.0 json-server: ^0.16.3 => 0.16.3 metro-react-native-babel-preset: ^0.66.0 => 0.66.1 (0.64.0) moment: ^2.29.1 => 2.29.1 patch-package: ^6.4.7 => 6.4.7 postinstall-postinstall: ^2.1.0 => 2.1.0 prettier: ^2.3.1 => 2.3.2 prettier-eslint: ^12.0.0 => 12.0.0 pretty-quick: ^3.1.1 => 3.1.1 qs: ^6.10.1 => 6.10.1 (6.7.0, 6.9.4, 6.5.2) react: 17.0.2 => 17.0.2 react-native: 0.64.2 => 0.64.2 react-native-animatable: ^1.3.3 => 1.3.3 react-native-calendars: ^1.1260.0 => 1.1264.0 react-native-date-picker: ^3.3.0 => 3.3.2 react-native-dotenv: ^2.6.2 => 2.6.2 react-native-easy-rating: ^0.2.1 => 0.2.1 react-native-email: ^1.1.0 => 1.1.0 react-native-exit-app: ^1.1.0 => 1.1.0 react-native-fast-image: ^8.3.4 => 8.3.6 (8.3.4) react-native-fbsdk-next: ^4.3.1 => 4.3.1 react-native-gesture-handler: ^1.10.2 => 1.10.3 react-native-gifted-chat: ^0.16.3 => 0.16.3 react-native-global-props: ^1.1.5 => 1.1.5 react-native-image-modal: ^2.0.3 => 2.0.3 react-native-image-picker: ^4.0.3 => 4.0.4 react-native-image-zoom-viewer: ^3.0.1 => 3.0.1 react-native-kakao-links: ^1.0.6 => 1.0.6 react-native-keyboard-aware-scroll-view: ^0.9.3 => 0.9.4 react-native-linear-gradient: ^2.5.6 => 2.5.6 react-native-modal: ^11.7.0 => 11.10.0 react-native-offline: ^5.8.0 => 5.8.0 react-native-paper: ^4.9.1 => 4.9.2 react-native-ratings: ^8.0.4 => 8.1.0 react-native-reanimated: ^2.2.0 => 2.2.0 react-native-safe-area-context: ^3.1.9 => 3.2.0 react-native-screens: ^3.4.0 => 3.4.0 react-native-send-intent: ^1.2.3 => 1.2.3 react-native-sensitive-info: ^5.5.8 => 5.5.8 react-native-splash-screen: ^3.2.0 => 3.2.0 react-native-status-bar-height: ^2.6.0 => 2.6.0 react-native-svg: ^12.1.1 => 12.1.1 react-native-swipeout: ^2.3.6 => 2.3.6 react-native-swiper: ^1.6.0 => 1.6.0 react-native-webview: ^11.6.4 => 11.6.5 react-navigation: ^4.4.4 => 4.4.4 react-test-renderer: 17.0.2 => 17.0.2 rn-fetch-blob: ^0.12.0 => 0.12.0 rn-tourguide: ^2.7.1 => 2.7.1 semver: ^7.3.5 => 7.3.5 (6.3.0, 7.0.0, 5.7.1) styled-components: ^5.2.1 => 5.3.0 styled-components/macro: undefined () styled-components/native: undefined () styled-components/primitives: undefined () typescript: ^4.3.4 => 4.3.5 (3.9.10) typescript-styled-plugin: ^0.18.0 => 0.18.0 npmGlobalPackages: @aws-amplify/cli: 5.2.1 dependency-cruiser: 10.0.6 npm: 7.19.1 react-devtools: 4.14.0 yarn: 1.22.11 ```

Describe the bug

Data is input to dynamodb at a specific time through a pre-scheduled lambda, and a push notification is sent to pinpoint through a lambda that is triggered when an insert occurs in the table. We sent about 1400 notifications today, and some of our customers are getting push notifications infinitely.(Almost every 1-2 minutes)

Expected behavior

Push notifications are not delivered repeatedly.

Reproduction steps

  1. From a lambda scheduled to run at a specific time, creates new data in dynamodb(using mutation, gql, etc).
  2. Another lambda function linked by the dynamodb trigger is executed.
  3. lambda's pinpoint send push notification
  4. On the customer's device the same push notifications are fired repeatedly every 1-2 minutes.

Code Snippet

Lambda function linked by the dynamodb trigger

exports.handler = async (event, context, callback) => {
  console.log('Event: ', JSON.stringify(event, null, 2));
  const records = event.Records.map(record => ({
    eventName: record.eventName,
    new: AWS.DynamoDB.Converter.unmarshall(record.dynamodb.NewImage),
    // old: AWS.DynamoDB.Converter.unmarshall(record.dynamodb.OldImage),
  }));

  for (var i = records.length; i--;) {
    const record = records[i];
    console.log('Stream record: ', `[${record.eventName}] `, JSON.stringify(record, null, 2));

    if (record.eventName === 'INSERT') {
      const userID = record.new.userID;
      const message = record.new.text;
      const sendMessage = (record.new.sender || '') === 'system';
      const isBot = record.new.user.name === 'Z-Chat';

      // console.log(userID, message, isBot, sendMessage);

      if (isBot && sendMessage) {
        const defaultMessage = {
          Action: 'OPEN_APP',
          Body: message,
          SilentPush: false,
          Title: '집현전AI',
          Data: {
            action: 'OPEN_ZCHAT',
          },
        };

        const sendMessagesParams = {
          ApplicationId: projectId,
          SendUsersMessageRequest: {
            Users: { [userID]: {} },
            MessageConfiguration: {
              DefaultPushNotificationMessage: {
                ...defaultMessage,
              },
              APNSMessage: {
                ...defaultMessage,
                TimeToLive: 0,
                Badge: 1,
                Sound: 'default',
              },
              GCMMessage: {
                TimeToLive: 0,
                Sound: 'default',
              }
            },
          },
        };

        await new Promise((resolve, reject) => {
          pinpoint.sendUsersMessages(
            sendMessagesParams,
            (sendMessagesErr, sendMessagesData) => {
              if (sendMessagesErr) {
                console.log('ERR: ', sendMessagesErr)
                reject(sendMessagesErr);
              }
              if (sendMessagesData) {

                resolve(sendMessagesData);
              }
            },
          );
        });
      }
    }
  }

  callback(null, `Successfully processed ${event.Records.length} records.`);
};

React Native

if (Platform.OS === 'android') {
  NativeModules.RNPushNotification.getToken(async (token: any) => {
    await AsyncStorage.setItem('pinpointToken', token);
  });
}

// get the registration token
// This will only be triggered when the token is generated or updated.
PushNotification.onRegister(async (token: any) => {
  await AsyncStorage.setItem('pinpointToken', token);
});

PushNotification.onNotification(
  (notification: { finish: (arg0: string) => void }) => {
    if (Platform.OS === 'ios') {
      notification.finish(PushNotificationIOS.FetchResult.NoData);
    }
  },
);

PushNotification.onNotificationOpened(async (notification: any) => {
  try {
    let conversationID;
    let action;

    const payload =
      notification?._data?.data?.jsonBody ||
      notification?.data['pinpoint.jsonBody'];

    if (payload) {
      const parsed =
        typeof payload === 'string' ? JSON.parse(payload) : payload;

      conversationID = parsed?.conversationID;
      action = parsed?.action;
    }

    ... Business logic
  } catch (e) {
    console.error('ERR', e);
  }
});

if (Platform.OS === 'ios') {
  PushNotification.requestIOSPermissions();
  PushNotificationIOS.setApplicationIconBadgeNumber(0);
}

Log output

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

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

tannerabread commented 1 year ago

@Parkseokje are you still running into this issue?

tannerabread commented 1 year ago

Hi 👋 Closing this as we have not heard back from you and I have not seen this elsewhere. If you are still experiencing this issue and in need of assistance, please feel free to comment and provide us with any information previously requested by our team members so we can re-open this issue and be better able to assist you.

Thank you!