Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pinpoint/FCM returning Unregistered or expired token

I'm using Pinpoint to push notifications through FCM and I'm receiving an error back from AWS:

{
    "ApplicationId": "xxx",
    "RequestId": "yyy",
    "EndpointResult": {
        "5551212": {
            "DeliveryStatus": "PERMANENT_FAILURE",
            "StatusCode": 410,
            "StatusMessage": "{\"errorMessage\":\"Unregistered or expired token\",\"channelType\":\"GCM\",\"pushProviderStatusCode\":\"200\",\"pushProviderError\":\"InvalidRegistration\",\"pushProviderResponse\":\"{\\\"multicast_id\\\":752174934090126,\\\"success\\\":0,\\\"failure\\\":1,\\\"canonical_ids\\\":0,\\\"results\\\":[{\\\"error\\\":\\\"InvalidRegistration\\\"}]}\"}",
            "Address": "userID"
        }
    }

An oddity is that when the app is launched/loaded the Amplify.config is not calling the PushNotification.onRegister function either:

const amplifyConfig = {
      Auth: {
        identityPoolId: POOL_ID,
        region: 'us-east-1'
      },
      Analytics: {
        AWSPinpoint: {
              appId: APP_ID,
              region: 'us-east-1',
              mandatorySignIn: false,
              endpointId: '5551212',
              endpoint: { 
                address: 'userID',
                channelType: 'GCM',
                optOut: 'NONE'
              }
        }
      }
    }

    PushNotification.onRegister(t => console.log(`Registration token: ${t}`), onRegister && onRegister());
    PushNotification.onNotification(n => (console.log(n), onNotification && onNotification(n)));
    PushNotification.onNotificationOpened(n => (console.log(n), onNotificationOpened && onNotificationOpened(n)));
    Amplify.configure(amplifyConfig);
like image 335
Patrick Dench Avatar asked Oct 21 '25 05:10

Patrick Dench


1 Answers

Edit: Your error seems related to Invalid Registration token: Make sure the endpoint address matches the registration token the client app receives from registering with FCM - https://developers.google.com/cloud-messaging/http-server-ref#error-codes).

I managed to make it work after login by getting the deviceToken from AsyncStorage.

If you want to keep the endpointId and only update the userId (only one user is logged in at each time - remember you can send push notifications to a specific userId that can have multiple endpoints (devices, email, phone number)):

try {
  const deviceToken = await AsyncStorage.getItem('push_token'+aws_exports.aws_mobile_analytics_app_id)
  if (deviceToken !== null) {
    console.log('device token from AsyncStorage', deviceToken)
    Analytics.updateEndpoint({
      optOut: 'NONE',
      channelType: 'GCM',
      userId: userId,
      address: deviceToken,
    })
  }
} catch (error) {
  console.log('error retrieving device token from AsyncStorage', error)
}

OR, if you want to specify your own endpointId (this way you can have multiple users/endpoints in the same device):

try {
  const deviceToken = await AsyncStorage.getItem('push_token'+aws_exports.aws_mobile_analytics_app_id)
  if (deviceToken !== null) {
    console.log('device token from AsyncStorage', deviceToken)
    Analytics.configure({
      disabled: false,
      AWSPinpoint: {
        appId: aws_exports.aws_mobile_analytics_app_id,
        region: aws_exports.aws_mobile_analytics_app_region,
        endpointId: endpointId,
        endpoint: {
          address: deviceToken,
          channelType: 'GCM',
          optOut: 'NONE',
          userId: userId
        }
      }
    })
    Analytics.updateEndpoint({
      optOut: 'NONE',
    })
  }
} catch (error) {
  console.log('error retrieving device token from AsyncStorage', error)
}

First, check your debug messages using window.LOG_LEVEL='DEBUG'

Then, make sure Analytics is working! Configure the Analytics module before the Push Notification module (https://aws-amplify.github.io/docs/js/push-notifications#configure-your-app). Do you have a call to PushNotification.configure()?

As far as I know, you need to have PushNotification.onRegister() called to get a valid active targetable endpoint.

Are you testing in a real device?

And what happens if you don't set the endpointId and endpoint attributes on your amplifyConfig? It should update your endpoint address with the device token on its own. You can later on update your endpoint with the user id Analytics.updateEndpoint({optOut: 'NONE', UserId: 'xxx'})

ps.: I was having a related issue and now is finally working, but I set up my backend with Amplify CLI so it may be slightly different

like image 76
Gabriela Thumé Avatar answered Oct 22 '25 22:10

Gabriela Thumé



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!