在 react-native 应用程序闲置一段时间后,推送通知会给出错误“未注册或过期的令牌”对于 Android

问题描述

我正在使用 aws pinpoint 处理本机推送通知。我没有使用 amplify cli 创建项目。我创建了 react-native 项目并拥有自己的 AWS 基础设施。 以下是我用于推送通知内容

  1. 放大 Cognito 身份验证和授权(在我的代码中实现)
  2. @aws-amplify/pushnotification(在我的代码中实现)
  3. 在 AWS 上查明项目
  4. 用于推送通知的 FCM。

以下是我在 react-native 中使用的配置:

const amplifyConfig = {
      Auth: {
        // required only for Federated Authentication - Amazon Cognito Identity Pool ID
        identityPoolId: "identityPoolId",// required - Amazon Cognito Region
        region: "region",// OPTIONAL - Amazon Cognito Federated Identity Pool Region 
        // required only if it's different from Amazon Cognito Region
        identityPoolRegion: "region",// OPTIONAL - Amazon Cognito User Pool ID
        userPoolId: "userPoolId",// OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
        userPoolWebClientId: "userPoolWebClientId",},Analytics: {
      disabled: false,AWSPinpoint: {
          appId: "PinPointApplicationId",region: "PinPointRegion",mandatorySignIn: false,// endpoint: {
          //     attributes: {
          //         hobbies: ['piano','hiking'],//     },//     channelType: 'GCM',// The channel type. Valid values: APNS,GCM
          // },}
      },}

// get the registration token
// This will only be triggered when the token is generated or updated.
PushNotification.onRegister((token) => {
  console.log('in app registration,token stored ',token);
});

PushNotification.configure({
  appId: "PinPointApplicationId",});


Amplify.configure(amplifyConfig);

Analytics.configure({
  appId: "PinPointApplicationId",});


function App(){
PushNotification.onNotification((notification) => {
    if (notification.foreground) {
      console.log('onNotification foreground',notification);
    } else {
        console.log('onNotification background or closed',notification);
    }
  });
  
  // get the notification data when notification is opened
  PushNotification.onNotificationopened((notification) => {
    if(notification && notification !== undefined  && notification.data !== undefined ){
    }
  });
}

我注意到一件事,PushNotification.onRegister() 永远不会被调用并且应用程序无法将设备令牌注册到 Pinpoint,所以我使用以下代码直接从 RNPushNotification 获取 devicetoken:

const getDevicetoken = async () => {
    
    NativeModules.RNPushNotification.getToken(async (token) => {
      await AsyncStorage.setItem('AWSMOBILE_TOKEN',token);
    });
}

用户提供有效凭据时,我登录用户并使用以下代码更新端点:

 let devicetoken = await AsyncStorage.getItem('AWSMOBILE_TOKEN');
if(devicetoken){
        await Analytics.updateEndpoint({
          address: devicetoken,channelType: "GCM",userId: uid,optOut: 'NONE',immediate: true,})
        .then((data) => {
          console.log('Update Endpoint: ',data);
          const myendpointId = Analytics.getPluggable('AWSPinpoint')
            ._config.endpointId;
          console.log('endpointId ++++++++++++++++ ',myendpointId);
        })
        .catch((error) => {
          console.log('Update Endpoint Error: ',error);
        });
        console.log("Endpoint updated with device token ",devicetoken);
      }

它可以正常工作并在我的设备上收到通知但问题是,大约 30 分钟后,当我尝试推送通知时,我得到了

Response:
"id": {
                    "Address": "address","DeliveryStatus": "PERMANENT_FAILURE","StatusCode": 410,"StatusMessage": "{"errorMessage":"Unregistered or expired token","channelType":"GCM","pushProviderStatusCode":"200","pushProviderError":"NotRegistered",\"pushProviderResponse":"{"results":[{"error":"NotRegistered"}],"multicast_id":"id","success":0,"failure":1,"canonical_ids":0}"}"}

有同样问题的朋友请帮帮我。我这几天都在挣扎。

提前致谢。 穆尼什

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)