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.39k stars 2.11k forks source link

Pinpoint deletes user endpoints when React-Native app is closed #12568

Open MensurRasic opened 7 months ago

MensurRasic commented 7 months ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Analytics

Amplify Categories

analytics, notifications

Environment information

``` System: OS: macOS 13.4 CPU: (10) arm64 Apple M1 Pro npmPackages: @aws-amplify/analytics: ^6.0.16 => 6.3.2 @aws-amplify/rtn-push-notification: ^1.1.1 => 1.1.1 @babel/core: ^7.12.9 => 7.22.8 @babel/runtime: ^7.12.5 => 7.22.6 @react-native-async-storage/async-storage: ^1.19.0 => 1.19.0 @react-native-community/eslint-config: ^2.0.0 => 2.0.0 @react-native-community/netinfo: ^8.3.1 => 8.3.1 @react-native-community/push-notification-ios: ^1.10.1 => 1.11.0 amazon-cognito-identity-js: ^5.2.14 => 5.2.14 (6.3.1) amazon-cognito-identity-js/internals: undefined () aws-amplify: ^5.3.3 => 5.3.3 aws-amplify-react-native: ^7.0.2 => 7.0.2 aws-sdk: ^2.1220.0 => 2.1413.0 axios: ^1.3.1 => 1.4.0 (0.26.0) babel-jest: ^26.6.3 => 26.6.3 card-validator: ^8.1.1 => 8.1.1 geolib: ^3.3.3 => 3.3.4 i: ^0.3.7 => 0.3.7 i18next: ^21.8.14 => 21.10.0 jest: ^26.6.3 => 26.6.3 metro-react-native-babel-preset: ^0.70.3 => 0.70.4 (0.76.7, 0.76.5) moment: ^2.29.4 => 2.29.4 npm: ^9.2.0 => 9.8.0 react: 18.2.0 => 18.2.0 react-devtools: 4.28.0 => 4.28.0 react-devtools-core: 4.28.0 => 4.28.0 react-native: ^0.72.4 => 0.72.4 ```

Describe the bug

I have a react native app using amplify analytics/notifications to push notifications to users. But there seems to be a bug where pinpoint delete constantly user endpoints without any specific logic when I open the app and kill it instantly.

This is impacting our user experience since we cannot be consistent with our logic regarding push notifications. I reproduced this bug and everytime I kill the app, reopen it, the user endpoints are deleted for further new notifications.

Expected behavior

Pinpoint shouldn't be deleting user endpoints when the user close the react-native app.

Reproduction steps

  1. Create a react native app.
  2. Install amplify analytics, notifications and cognito auth.
  3. Setup to push notification to the specific authenticated user.
  4. Kill the react native app.
  5. Open the react native app.
  6. Kill it again.
  7. Observe the deleted user endpoints.

Code Snippet

// Put your code below this line.

Log output

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

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

iPhone 8, iPhone XR, iPhone 14 pro

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

MensurRasic commented 7 months ago

To add more context to this ticket, here is to operations we do that lead to the bug.

We want to push a notification to a user based on a lambda trigger. The lambda get the userID who inserted the new object in dynamoDB that triggered the lambda. Then we get the user endpoints with the function PINPOINT.getUserEndpoints. When the app is opened for the first time, the lambda function works great, but when we kill the appm suddently the lambda throws the following error : NotFoundException: Resource not found

But, we can still send and receive notification through the pinpoint test messaging console.

cwomack commented 7 months ago

Hello @MensurRasic, and thank you for providing detailed context and reproduction steps. Just to get further details on reproducing the issue, are you closing down the app before any events are flushed from the buffer?

MensurRasic commented 7 months ago

@cwomack , it doesn't matter when I close the app, I can wait 2 seconds before closing it or 5 minutes, the bug still remains. Also, I just found this thread. The user ahtokca explains exactly the same behaviour I get on my side.

cwomack commented 7 months ago

@MensurRasic, can you clarify if you're using authenticated or unauthenticated users? In the event of unauthenticated users, it's possible that Push Notifications still work because of an endpoint being tied to the device token... but the killing of the app then causes the user to be incorrect once it's rebooted.

MensurRasic commented 7 months ago

@cwomack In my scenario I was using an authenticated user.. And the user session was still there even after closing/re-opening the app. I was not able to retrieve user endpoints after the second time I kill the app. But here are my findings and alternative I took.

Observation I setup a lambda function with a trigger to be executed each time a new item was inserted into MessageTable to push a notification to a user. To push a notification, I was using the function SendUsersMessages but it was throwing the error NotFoundException: Resource not found.

So I decided to retrieve endpoints before sending the notification using the pinpoint function GetUserEndpoints, in order to get all the user endpoints and send the user a notification on all his devices... But unfortunately, there seem to be a problem with those pinpoint functions to retrieve endpoints by passing the userID. As mentioned, after the second time I was killing the mobile app, the function GetUserEndpoints was throwing the error NotFoundException: Resource not found...

So I took the endpointID that was working initially on the first run (before killing the app twice) and I was able to push notification to endpoints using the AWS Pinpoint console. So I was wondering how it was possible for the device to receive those notifications from the pinpoint console but the function GetUserEndpoints was not able to retrieve the endpoints...

So I wrote another function, but this time I was using SendMessages function.. and it was working 100% of the time even when GetUserEndpoints was throwing the error...

Alternative Since SendMessages was working all the time, I am now storing the endpointID in the DynamoDB each time the user authenticate using the lambda function post authentication and I am sending notifications only on the last device the user is authenticated...


The alternative is working great on iOS and Android... but the pinpoint functions mentioned above are clearly buggy and not working properly as suggested by the documentation.


Hopefully this can give you a more insight on the bug.

cwomack commented 2 months ago

@MensurRasic, we are looking into this again after thinking it may be related to what's reported on the v6 side within #13174. Not sure if you've had the chance to upgrade to v6, but curious if you're seeing the same behavior still in v6 that is reported in that issue. Would love to get any additional context or confirmation from you if so!