定位/FCM 返回未注册或已过期的令牌

4
我正在使用Pinpoint通过FCM推送通知,但是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"
        }
    }

一个奇怪的现象是,当应用程序被启动/加载时,Amplify.config也不会调用PushNotification.onRegister函数:
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);

你解决了这个问题吗? - Fook
还没有解决这个问题... - Patrick Dench
这个有任何更新吗??? - Aslam
我最终编写了一个模块直接获取令牌。 - Patrick Dench
嗨,我试着直接从Firebase获取令牌,一旦登录成功,就使用以下配置更新设备令牌的终端节点:await Analytics.updateEndpoint({ // address: fcmToken, channelType: "GCM", userId: uid, optOut: 'NONE', });看起来它起作用了,我可以向我的设备发送推送通知。但是在大约20分钟后,如果我试图再次发送通知,我会得到以下错误: "StatusCode": 410, "StatusMessage": "{"errorMessage":"未注册或已过期的令牌 有任何想法为什么会出现这个问题? - Munish
1个回答

2

编辑:您的错误似乎与无效的注册令牌有关:请确保端点地址与客户端应用程序从使用 FCM 进行注册时接收到的注册令牌相匹配-https://developers.google.com/cloud-messaging/http-server-ref#error-codes)。

我成功地通过从AsyncStorage中获取deviceToken来使其在登录后工作。

如果您想保留endpointId并仅更新userId(每次只有一个用户登录-请记住,您可以向特定的userId发送推送通知,该用户可以具有多个端点(设备、电子邮件、电话号码)):

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)
}

或者,如果你想指定自己的endpointId(这样你可以在同一设备上拥有多个用户/端点):

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)
}

首先,使用window.LOG_LEVEL='DEBUG'检查您的调试消息。

然后,请确保分析功能正常工作!在配置推送通知模块之前,请配置分析模块(https://aws-amplify.github.io/docs/js/push-notifications#configure-your-app)。您是否调用了PushNotification.configure()

据我所知,需要调用PushNotification.onRegister()来获取有效的可定位端点。

您是在真实设备上进行测试吗?

如果您没有在amplifyConfig上设置endpointIdendpoint属性会发生什么?它应该自动使用device token更新您的端点地址。稍后,您可以使用用户ID更新您的端点Analytics.updateEndpoint({optOut: 'NONE', UserId: 'xxx'})

附注:我曾遇到相关问题,现在终于解决了,但我是使用Amplify CLI设置我的后端,因此可能略有不同。


如果我删除以下内容 endpointId: '5551212', endpoint: { address: 'userID', channelType: 'GCM', optOut: 'NONE' }它会注册设备ID,我可以向其发送消息,但我无法在updateEndpoint函数调用中指定endpointId。 - Patrick Dench
哦,回答你的其他问题,我正在调用PushNotification.configure(),是的,我正在使用真实设备进行测试。我的困境在于,我想发送到一个端点ID或其他标识符,而不是设备ID,因为我们将在一个设备上有多个用户。 - Patrick Dench
嘿,@PatrickDench,我改进了我的答案,你能检查一下现在是否有帮助吗? - Gabriela Thumé
我会看一下 - 我暂时使用了一个RN模块来进行黑客式的解决方案。相当恶心,但是... - Patrick Dench

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接