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.45k stars 2.13k forks source link

Unregistered or expired token error in Android #13111

Closed seokjeatuiti closed 8 months ago

seokjeatuiti commented 8 months ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Push Notifications

Amplify Version

v5

Amplify Categories

notifications

Backend

Amplify CLI

Environment information

``` System: OS: macOS 14.1.1 CPU: (8) arm64 Apple M1 Pro Memory: 83.38 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 18.17.1 - /usr/local/bin/node Yarn: 1.22.19 - /opt/homebrew/bin/yarn npm: 10.1.0 - /opt/homebrew/bin/npm Watchman: 2023.09.04.00 - /opt/homebrew/bin/watchman Browsers: Chrome: 122.0.6261.112 Safari: 17.1 npmPackages: @aws-amplify/rtn-push-notification: ^1.2.19 => 1.2.19 (1.1.13) @aws-amplify/ui-components: ^1.9.40 => 1.9.40 @babel/cli: ^7.14.5 => 7.23.9 @babel/core: ^7.14.6 => 7.24.0 @babel/preset-env: ^7.14.5 => 7.24.0 @babel/preset-typescript: ^7.14.5 => 7.23.3 @babel/runtime: ^7.14.6 => 7.24.0 @expo/react-native-action-sheet: ^4.0.1 => 4.0.1 @miblanchard/react-native-slider: ^2.1.0 => 2.3.1 @react-native-async-storage/async-storage: ^1.18.2 => 1.22.3 @react-native-camera-roll/camera-roll: ^5.1.1 => 5.10.0 @react-native-clipboard/clipboard: ^1.11.2 => 1.13.2 @react-native-community/eslint-config: ^3.0.0 => 3.2.0 @react-native-community/hooks: ^3.0.0 => 3.0.0 @react-native-community/netinfo: ^9.3.7 => 9.5.0 @react-native-community/push-notification-ios: ^1.11.0 => 1.11.0 @react-native-firebase/analytics: ^12.1.0 => 12.9.3 @react-native-firebase/app: ^12.1.0 => 12.9.3 @react-native-masked-view/masked-view: ^0.2.9 => 0.2.9 @react-native/metro-config: 0.73.5 => 0.73.5 @react-navigation/bottom-tabs: ^5.11.8 => 5.11.15 @react-navigation/native: ^6.1.7 => 6.1.14 (3.8.4) @react-navigation/stack: ^6.2.0 => 6.3.25 @testing-library/react-hooks: ^7.0.2 => 7.0.2 @types/jest: ^26.0.23 => 26.0.24 @types/lodash: ^4.14.202 => 4.14.202 @types/qs: ^6.9.6 => 6.9.12 @types/react: ^17.0.39 => 17.0.76 (16.14.57) @types/react-native: ^0.72.2 => 0.72.8 (0.70.19) @types/react-native-global-props: ^1.1.0 => 1.1.6 @types/react-native-keyboard-spacer: ^0.4.1 => 0.4.6 @types/react-test-renderer: ^17.0.1 => 17.0.9 (18.0.7) @types/styled-components: ^5.1.10 => 5.1.34 @types/styled-components-react-native: ^5.1.1 => 5.2.5 @types/uuid: ^8.3.0 => 8.3.4 @typescript-eslint/eslint-plugin: ^4.27.0 => 4.33.0 (5.62.0) @typescript-eslint/parser: ^4.27.0 => 4.33.0 (5.62.0, 3.10.1) HelloWorld: 0.0.1 amazon-cognito-identity-js: ^5.0.3 => 5.2.14 (6.3.12) amazon-cognito-identity-js/internals: undefined () amplify-ui-components-loader: undefined () aws-amplify: ^5.0.18 => 5.3.17 aws-sdk: ^2.931.0 => 2.1573.0 axios: ^0.27.2 => 0.27.2 (1.6.7) babel-jest: ^27.0.2 => 27.5.1 (29.7.0) babel-plugin-root-import: ^6.6.0 => 6.6.0 css-tree: ^2.3.1 => 2.3.1 (1.1.3) dayjs: ^1.11.5 => 1.11.10 (1.8.26) deprecated-react-native-prop-types: ^4.2.1 => 4.2.3 (5.0.0) eslint: ^7.28.0 => 7.32.0 eslint-plugin-import: ^2.23.4 => 2.29.1 eslint-plugin-prettier: ^3.3.1 => 3.4.1 (4.2.1) graphql: ^15.5.0 => 15.8.0 (14.7.0) graphql-tag: ^2.12.4 => 2.12.6 graphql-ttl-transformer: ^1.1.0 => 1.1.1 husky: 6.0.0 => 6.0.0 iamport-react-native: ^2.0.2 => 2.0.12 jest: ^29.6.3 => 29.7.0 jest-mock-axios: ^4.4.0 => 4.7.3 loadsh: ^0.0.4 => 0.0.4 lottie-ios: ^4.3.3 => 4.4.1 lottie-react-native: ^6.3.1 => 6.7.0 metro-react-native-babel-preset: ^0.76.5 => 0.76.9 patch-package: ^6.4.7 => 6.5.1 postinstall-postinstall: ^2.1.0 => 2.1.0 prettier: 2.8.8 => 2.8.8 (3.2.5) prettier-eslint: ^12.0.0 => 12.0.0 pretty-quick: ^3.1.1 => 3.3.1 qs: ^6.10.1 => 6.12.0 (6.9.4) react: 18.2.0 => 18.2.0 react-native: 0.73.5 => 0.73.5 react-native-anchor-point: ^1.0.6 => 1.0.6 react-native-animatable: ^1.3.3 => 1.4.0 (1.3.3) react-native-auto-height-image: ^3.2.4 => 3.2.4 react-native-bootsplash: ^5.4.1 => 5.4.1 react-native-calendars: ^1.1303.0 => 1.1304.1 react-native-confirmation-code-field: ^7.3.1 => 7.3.2 react-native-contacts: ^7.0.7 => 7.0.8 react-native-date-picker: ^3.3.0 => 3.4.3 react-native-detect-press-outside: ^1.0.4 => 1.0.4 react-native-devsettings: ^1.0.5 => 1.0.5 react-native-document-scanner-plugin: ^0.9.1 => 0.9.1 react-native-dropdown-picker: ^5.4.6 => 5.4.6 react-native-email: ^1.1.0 => 1.1.0 react-native-exit-app: ^1.1.0 => 1.1.0 react-native-fast-image: ^8.6.1 => 8.6.3 react-native-fbsdk-next: ^11.1.0 => 11.3.0 react-native-geolocation-service: ^5.3.1 => 5.3.1 react-native-gesture-handler: ^2.15.0 => 2.15.0 react-native-gifted-chat: ^2.4.0 => 2.4.0 react-native-global-props: ^1.1.5 => 1.1.5 react-native-highlight-words: ^1.0.1 => 1.0.1 react-native-image-picker: ^7.0.0 => 7.1.0 react-native-image-slider-box: ^2.0.7 => 2.0.7 react-native-image-slider-show: ^1.0.3 => 1.0.3 react-native-image-viewing: ^0.2.2 => 0.2.2 react-native-image-zoom-viewer: ^3.0.1 => 3.0.1 react-native-inset-shadow: ^1.0.3 => 1.0.3 react-native-kakao-share-link: ^1.0.10 => 1.0.10 react-native-keyboard-accessory: ^0.1.16 => 0.1.16 react-native-keyboard-aware-scroll-view: ^0.9.5 => 0.9.5 react-native-linear-gradient: ^2.5.6 => 2.8.3 react-native-modal: ^13.0.0 => 13.0.1 react-native-pager-view: ^6.2.1 => 6.2.3 react-native-paper: ^4.9.1 => 4.12.8 react-native-permissions: ^3.10.0 => 3.10.1 react-native-progress: ^5.0.0 => 5.0.1 react-native-pull-to-refresh-custom: ^1.0.3 => 1.0.3 react-native-safe-area-context: ^4.5.3 => 4.9.0 react-native-screens: ^3.29.0 => 3.29.0 react-native-send-intent: ^1.2.3 => 1.3.0 react-native-sensitive-info: ^5.5.8 => 5.5.8 react-native-shadow-2: ^7.0.8 => 7.0.8 react-native-sound: ^0.11.2 => 0.11.2 react-native-sse: ^1.2.0 => 1.2.1 react-native-star-rating-widget: ^1.7.3 => 1.7.3 react-native-status-bar-height: ^2.6.0 => 2.6.0 react-native-svg: ^13.9.0 => 13.14.0 react-native-swipeout: ^2.3.6 => 2.3.6 react-native-url-polyfill: ^2.0.0 => 2.0.0 (1.3.0) react-native-webview: 11.23.1 => 11.23.1 react-native-wheely: ^0.6.0 => 0.6.0 react-native-youtube-iframe: ^2.3.0 => 2.3.0 react-navigation: ^4.4.4 => 4.4.4 react-test-renderer: 18.2.0 => 18.2.0 rn-fetch-blob: ^0.12.0 => 0.12.0 semver: ^7.3.5 => 7.6.0 (6.3.1, 5.7.2) styled-components: ^5.2.1 => 5.3.11 styled-components/macro: undefined () styled-components/native: undefined () styled-components/primitives: undefined () typescript: 5.0.4 => 5.0.4 (3.9.10) typescript-styled-plugin: ^0.18.0 => 0.18.3 npmGlobalPackages: @aws-amplify/cli: 12.6.0 eslint: 8.27.0 n: 9.1.0 node-gyp: 9.1.0 npm: 10.1.0 yarn: 1.22.19 ```

Describe the bug

There is an issue where notifications keep disconnecting on several Android devices. What's a little strange is that notifications come well when the app is in the background, but once the app is in terminated state, only about 5-6 notifications come and then no more.

Once we reactivate or reinstall the app, it comes and goes a few more times and then disconnects again. Sometimes it comes without interruption, but it doesn't last long.

When I check the pinpoint endpoints using aws cli, they all show as active, but when I test them with send-users-messages, the error below occurs. If I send a test message through the firebase console, it comes in fine.

{
  "c80832bf-9266-40ae-ba80-51403d715be7": {
    "60ff3aa4-9f17-4649-9f14-0a1a8b6a5fe1": {
      "Address": "eHrVNfunTLeY22Oc67XxJG:APA91bH5ye424wSEeKfUGT6ijBQ4jWTG65OeIwZXI8-w9Nu3AaS9qGu1H7uX3NE4DoySzLVS1vUDueFEqr6ZPACLEBwxqeCjEDSZi2ztUNIguO9_t62yTBcjXmiBARwQKaJoQttWSEwc",
      "DeliveryStatus": "PERMANENT_FAILURE",
      "StatusCode": 410,
      "StatusMessage": {
        "errorMessage": "Unregistered or expired token",
        "channelType": "GCM",
        "pushProviderStatusCode": "200",
        "pushProviderError": "NotRegistered",
        "pushProviderResponse": {
          "multicast_id": "6284102397725547368",
          "success": 0,
          "failure": 1,
          "canonical_ids": 0,
          "results": [
            {
              "error": "NotRegistered"
            }
          ]
        }
      }
    }
  }
}

Expected behavior

Notifications should come continuously.

Reproduction steps

N/A

Code Snippet

// Put your code below this line.

Log output

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

aws-exports.js

/* eslint-disable */
// WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.

const awsmobile = {
    "aws_project_region": "ap-northeast-2",
    "aws_appsync_graphqlEndpoint": "https://....appsync-api.ap-northeast-2.amazonaws.com/graphql",
    "aws_appsync_region": "ap-northeast-2",
    "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
    "aws_cloud_logic_custom": [
        {
            "name": "BtakRest",
            "endpoint": "https://....execute-api.ap-northeast-2.amazonaws.com/dev",
            "region": "ap-northeast-2"
        },
        {
            "name": "BtalkAdminRest",
            "endpoint": "https://....execute-api.ap-northeast-2.amazonaws.com/dev",
            "region": "ap-northeast-2"
        },
        {
            "name": "RestClient",
            "endpoint": "https://....execute-api.ap-northeast-2.amazonaws.com/dev",
            "region": "ap-northeast-2"
        }
    ],
    "aws_cognito_identity_pool_id": "ap-northeast-2:...",
    "aws_cognito_region": "ap-northeast-2",
    "aws_user_pools_id": "ap-northeast-.,..
    "aws_user_pools_web_client_id": "...",
    "oauth": {},
    "aws_cognito_username_attributes": [],
    "aws_cognito_social_providers": [],
    "aws_cognito_signup_attributes": [
        "PHONE_NUMBER"
    ],
    "aws_cognito_mfa_configuration": "OFF",
    "aws_cognito_mfa_types": [
        "SMS"
    ],
    "aws_cognito_password_protection_settings": {
        "passwordPolicyMinLength": 8,
        "passwordPolicyCharacters": []
    },
    "aws_cognito_verification_mechanisms": [
        "PHONE_NUMBER"
    ],
    "aws_mobile_analytics_app_id": "...",
    "aws_mobile_analytics_app_region": "us-west-2",
    "Analytics": {
        "AWSPinpoint": {
            "appId": "...",
            "region": "us-west-2"
        }
    },
    "Notifications": {
        "Push": {
            "AWSPinpoint": {
                "appId": "...",
                "region": "us-west-2"
            }
        }
    },
    "aws_user_files_s3_bucket": "xxx",
    "aws_user_files_s3_bucket_region": "ap-northeast-2"
};

export default awsmobile;

Manual configuration

No response

Additional configuration

No response

Mobile Device

Galaxy S23+, Galaxy A53 5G, Pixel2 (API 33, Emulator)

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

cwomack commented 8 months ago

Hello, @seokjeatuiti 👋. Can you clarify what version of Android you're using and if you're seeing this on both physical devices and emulators? I wasn't able to reproduce this on more recent versions of Android, so if you could also provide your frontend code that's reproducing this issue it would be appreciated! I think we need to know where you're calling the Push Notification API's in the notification lifecycle/state of the app.

seokjeatuiti commented 8 months ago

@cwomack

Thank you for answer! We have provided answers for each item you asked.

Can you clarify what version of Android you're using and if you're seeing this on both physical devices and emulators?

Yes it's happening on both physical device and emulator.

I wasn't able to reproduce this on more recent versions of Android, so if you could also provide your frontend code that's reproducing this issue it would be appreciated!

When notifications were being cut off, the settings were as follows. For reference, We provide both OTP for general users and USER_SRP_AUTH authentication methods for employees.

import { Amplify } from '@aws-amplify/core';
import awsConfig from './aws-exports';
import { Auth, Notifications, Analytics } from 'aws-amplify';

Amplify.configure({
  ...awsConfig,
  Auth: {
    authenticationFlowType: 'USER_SRP_AUTH',
  },
  Analytics: {
     disabled: false
  },
});

  Notifications.Push.enable();
};

I was suspicious that Analytics might be causing a problem with the endpoint, so I added Analytics.disable(), notifications started coming in fine from then on. I referred to this document.

Amplify.configure({
  ...awsConfig,
  Auth: {
    authenticationFlowType: 'USER_SRP_AUTH',
  },
  // Analytics: {
  //   disabled: false
  // },
});

  Analytics.disable();
  Notifications.Push.enable();
};

The identifyUser part is as follows. optOut is not separately specified in the v5 official document, but when this part was excluded at first, there was a problem with the endpoint being created as optOut: All.

export const updatePushToken = async (userId: string) => {
  const token = await AsyncStorage.getItem('pinpointToken');
  if (token) {
    try {
      const userProfile: AWSPinpointUserInfo = {
        optOut: 'NONE',
      };
      await Notifications.Push.identifyUser(userId, userProfile);
    } catch (e) {
      console.error('updateEndpoint ERR', e);
    }
  }
};

I think we need to know where you're calling the Push Notification API's in the notification lifecycle/state of the app

We are sending notifications using pinpoint SDK through Lambda. There doesn't seem to be any particular problem in this part.

const AWS = require('aws-sdk');
const pinpoint = new AWS.Pinpoint({ region: 'us-west-2' });
...

const defaultMessage = {
  Action: 'OPEN_APP',
  Body: message,
  SilentPush: false,
  Title: senderName || '{Service Name}',
  Data: {
    conversationID,
  },
};

const sendMessagesParams = {
    ApplicationId: projectId,
    SendUsersMessageRequest: {
      Users: notifiedUsers,
      MessageConfiguration: {
        DefaultPushNotificationMessage: {
          ...defaultMessage,
        },
        APNSMessage: {
          Badge: 1,
          Priority: '10',
          Sound: 'default',
          ThreadId: senderId,
        },
        GCMMessage: {
          Priority: 'high',
          Sound: 'default',
        },
      },
    },
  };

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

By the way, this is an old post, but I experienced a similar problem once in the past, and registered an issue at that time. Please note.

nitbk502 commented 8 months ago

Hello, I am encountering the same problem. Here are the CloudWatch logs: 2024-03-29T16:30:45.379Z 6a9370ac-fa6e-5925-8e45-2e2c148de650 INFO recipientResult { 'f1c91d2a-1236-408e-9d89-823185a51140': { DeliveryStatus: 'PERMANENT_FAILURE', StatusCode: 410, StatusMessage: '{"errorMessage":"Unregistered or expired token","channelType":"GCM","pushProviderStatusCode":"200","pushProviderError":"NotRegistered","pushProviderResponse":"{\\"multicast_id\\":\\"2421628289060879526\\",\\"success\\":0,\\"failure\\":1,\\"canonical_ids\\":0,\\"results\\":[{\\"error\\":\\"NotRegistered\\"}]}"}', Address: 'e5J1QAnFTPC0UmggiGi2qr:APA91bH3bGv5G4AsKWX6uPqiH7_uSEe0y_SxDBLDl09r8XehJUaX9Pl3RcIGoZZ6D-pWX3qVAhykeNgCFMuF6Mosy9qL20TzBiP8zXN6zbHj88iO64TGjBTWWBu4dTFqgwOj6c--kys9' }, '5b13560a-f123-42d5-a58d-d04768d9246c': { DeliveryStatus: 'PERMANENT_FAILURE', StatusCode: 410, StatusMessage: '{"errorMessage":"Unregistered or expired token","channelType":"GCM","pushProviderStatusCode":"200","pushProviderError":"NotRegistered","pushProviderResponse":"{\\"multicast_id\\":\\"6799976235612541133\\",\\"success\\":0,\\"failure\\":1,\\"canonical_ids\\":0,\\"results\\":[{\\"error\\":\\"NotRegistered\\"}]}"}', Address: 'eOZAYcL7SdaJiEyflrezSi:APA91bEsxIdFA0Gxk64sZ-m9L_5LZkbmn-mmQhAu4tC1tXZ4QGm-KSDZjosqeJg-qXvXJn4Y1inNdc9ygzZTImZN50OWkvJ0udQjLHwWC7J_BOB7DeIwQvjvo9aIjOsmPWLTYbFxtB86' }, '35124492-4045-4a64-99fb-cb2774b05a00': { DeliveryStatus: 'PERMANENT_FAILURE', StatusCode: 410, StatusMessage: '{"errorMessage":"Unregistered or expired token","channelType":"GCM","pushProviderStatusCode":"200","pushProviderError":"NotRegistered","pushProviderResponse":"{\\"multicast_id\\":\\"1526660718498104442\\",\\"success\\":0,\\"failure\\":1,\\"canonical_ids\\":0,\\"results\\":[{\\"error\\":\\"NotRegistered\\"}]}"}', Address: 'eC-us18cQxaLZHJ__m7bEz:APA91bHaV6Y9P1aqo34fxgnyCnOun_13F0w4h2sE8VtDnieZMaj2srZbqwtkkuLTt72sF0F5o9_ebzH9Qn6JdnNdDUJzOoBIm3jtNBf3fElFYOpVrZoEzRnSBc2HpY2VWljOESD0YMZv' }, 'cc0b8944-2ccf-4fa0-8ca2-e36c4986a7cc': { DeliveryStatus: 'PERMANENT_FAILURE', StatusCode: 410, StatusMessage: '{"errorMessage":"Unregistered or expired token","channelType":"GCM","pushProviderStatusCode":"200","pushProviderError":"NotRegistered","pushProviderResponse":"{\\"multicast_id\\":\\"3866949699306559810\\",\\"success\\":0,\\"failure\\":1,\\"canonical_ids\\":0,\\"results\\":[{\\"error\\":\\"NotRegistered\\"}]}"}', Address: 'ejd8IArJSy2-WLmp1IzxEw:APA91bGpOcYzg8QsGqFvTq0-hec_wDh-XIb5hP9Btq5Og1Y-_vO1NtxlWIzD6va-wUiBODwrIoK6oa9-_hl84cJiaMLzGeVCIeU-zAtuuzrioqlb4n06kRHJQDLrr9gt3GiOjTXczVl6' } }

cwomack commented 8 months ago

@seokjeatuiti, is the token address in the error messages is the same one that you are using to send from the Firebase console? Any chance you can you share a couple of failures from the same device if possible? Curious to see if the errors are all showing the same address (or if anything changes).

seokjeatuiti commented 8 months ago

@cwomack, Every time I started a new test, I deleted all endpoints using cli to ensure a clean experiment. Of course, since the token address newly created on the device was used during the Firebase Console test, it is unlikely that the token address in the two environments changed or was altered before or after the test.

The gist of what I posted was that the endpoint would be deleted after sending 1-2 messages, and that PERMANENT_FAILURE would be displayed when looking up the endpoint information through cli. It's not a problem with the token address. Fortunately, nothing special has happened since adding Analytics.disable() (Please refer to the above).

In my opinion, this problem appears to have been caused by anomalous use of the Analytics module that does not match the version. I don't think we need to spend any more time on this issue. Therefore, I will close the ticket. Anyway, thank you.