首页 > 解决方案 > 在 react-native 中使用 aws pinpoint 推送通知

问题描述

我正在将推送通知从 AWS Pinpoint 发送到我的 react-native 应用程序。如果我使用 aws amplify CLI 开发一个新的 react-native 应用程序,如 AWS 在线指南中所述,并创建全新的设置,它可以正常工作。但问题是我已经在我的 react-native 应用程序中使用 Auth 进行了放大设置。现在我需要将 PUSH NOTIFICATION 集成到我现有的 react-native 应用程序中。如何在我现有的已配置 AWS AUTH 的 react-native 应用程序中使用 AWS PINPOINT 添加推送通知。

下面是我尝试过的事情。

  1. 创建 Firebase 项目并在 Android 中添加所需的 Android 依赖项和配置。
  2. 保存了 AWS Pinpoint 推送通知所需的 API 服务器密钥。
  3. 创建 AWS Pinpoint 项目并在推送通知中添加 FCM 通道。
  4. 在我的 react-native 应用程序的 Analytics 配置下方添加。

react-native 中的配置:

Amplify.configure({
  Auth: {
      // REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
      identityPoolId: 'ap-XXXXXX',

      // REQUIRED - Amazon Cognito Region
      region: 'ap-XXXXX',

      // OPTIONAL - Amazon Cognito Federated Identity Pool Region 
      // Required only if it's different from Amazon Cognito Region
      identityPoolRegion: 'ap-XXXX',

      // OPTIONAL - Amazon Cognito User Pool ID
      userPoolId: 'ap-XXXXX',

      // OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
      userPoolWebClientId: 'XXXXXXX',
  },
  Analytics: {
    AWSPinpoint: {
        // OPTIONAL -  Amazon Pinpoint App Client ID
        appId: 'XXXXX',
        // OPTIONAL -  Amazon service region
        region: 'us-west-2',
        // OPTIONAL -  Customized endpoint
        //endpointId: 'XXXXXXXXXXXX',
        // OPTIONAL - Default Endpoint Information
        endpoint: {
           // address: 'xxxxxxx', // The unique identifier for the recipient. For example, an address could be a device token, email address, or mobile phone number.
            attributes: {
                hobbies: ['piano', 'hiking'],
            },
            channelType: 'GCM', // The channel type. Valid values: APNS, GCM
            // Customized userId
            //userId: 'XXXXXXXXXXXX',
            // User attributes
            //userAttributes: {
              //  interests: ['football', 'basketball', 'AWS']
                // ...
            //}
        },
    }
  },
});

PushNotification.onNotification((notification) => {
  console.log('in app notification', notification);
});

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

// get the notification data when notification is opened
PushNotification.onNotificationOpened((notification) => {
    console.log('the notification is opened', notification);
});

Analytics.getPluggable('AWSPinpoint')._config.endpointId;
//above give me the endpoint.

我的应用程序运行良好。但是当我尝试从 AWS CLI 控制台发送通知时,我收到错误

AWS cli:
> aws pinpoint send-messages --cli-input-json file://test.json
An error occurred (NotFoundException) when calling the GetEndpoint operation: Resource not found

test.json:
{
  "ApplicationId": "pinpoint_app_client-id",
  "MessageRequest": {
    "Endpoints": {
      "endpoint_got_above": {}
    },
    "MessageConfiguration": {
      "DefaultPushNotificationMessage": {
        "Body": "Test Body",
        "Title": "Test Title"
      }
    }
  }
}

我是 AWS 的新手,有点卡在这里。我已经花了一整天的时间,但找不到问题所在。有什么我想念的吗?

请帮帮我。

谢谢,穆尼什

标签: amazon-web-servicesreact-nativefirebase-cloud-messagingaws-cliaws-pinpoint

解决方案


当调用 PushNotification onRegister 方法时,不知何故我没有得到 deviceToken/Endpoint。可能是 Pinpoint 没有填充 firebase 端点/令牌的原因。所以我不得不通过传递令牌来手动使用 updateEndpoint 。所以我尝试在我的项目中使用firebase并尝试获取令牌。我将相同的内容传递给了 Analytics updateEndpoint,它对我有用。基本上我没有调用 updateEndpoint ,因为我没有有效的端点来传递。

如果将来有人被卡住,请发布此内容。谢谢


推荐阅读