通过 AWS Pinpoint 向指定用户发送推送通知

Send push notifications via AWS Pinpoint to specified User

我正在使用 react-native 和 amplify 通过 AWS Pinpoint 向设备发送推送通知。我可以获得为设备生成的令牌。但我只需要使用用户 ID 发送推送通知。我尝试更新端点,但它不起作用。谁能建议我处理这个问题的正确方法?

PushNotification.onRegister((token) => {
  console.log('in app registration', token);
  Analytics.updateEndpoint({
    address: token,
    channelType: "GCM",
    OptOut: 'NONE',
    userId: "12345"
  }).then(data => {
    console.log(data)
  }).catch(error => {
    console.log(error)
  });
});

使用 Amazon Pinpoint,您无法将交易消息作为推送通知发送。这意味着,您不能将推送通知直接发送给特定的收件人。

Amazon Pinpoint - 推送通知支持通过创建活动和细分向目标受众发送通知。

如果仅用于测试,您可以在 Pinpoint 仪表板中使用用户 ID 或设备令牌向特定用户发送测试消息

在此处阅读更多内容 => Sending Transactional Messages from Your Apps - Amazon Pinpoint

我能够使用 @aws-amplify/analytics 库来做到这一点。以下是我使用的方法。

Analytics.configure(aws_exports);

PushNotification.onRegister((token) => {
        //alert(token) 
        console.log('in app registration', token);
        Analytics.updateEndpoint({
            address: token, // The unique identifier for the recipient. For example, an address could be a device token, email address, or mobile phone number.
            attributes: {
              // Custom attributes that your app reports to Amazon Pinpoint. You can use these attributes as selection criteria when you create a segment.
              hobbies: ['piano', 'hiking'],
              interests: ['basketball']
            },
            channelType: 'GCM', // The channel type. Valid values: APNS, GCM
            userId: '221XWsdfER234',
            // User attributes
            optOut: 'ALL',
            userAttributes: {
                interests: ['football', 'basketball', 'AWS']
                // ...
            }
        }).then((data) => {
            console.log(data)
        }).catch(error => {
            console.log(error)
        })

    });

这取决于您希望如何发送推送通知。我们创建了 UI 允许发送推送,触发 lambda。

首先,您需要应用程序像您一样使用令牌/地址更新端点。

然后您可以从 lambda 发送推送,如代码所示。

const sendPushNotification = async () => {
  const params = {
    ApplicationId: "PINPOINT_ANALYTICS_ID",
    SendUsersMessageRequest: {
      Users: { 
        "12345": {} // replace numbers with userid here connected with pinpoint endpoint
      },
      MessageConfiguration: {
        APNSMessage: {
          Action: 'OPEN_APP',
          Title: 'Title of push notification',
          SilentPush: false,
          Sound: 'default',
          Body: 'Message you would like to send'
        },
        GCMMessage: {
          Action: 'OPEN_APP',
          Title: 'Title of push notification',
          SilentPush: false,
          Sound: 'default',
          Body: 'Message you would like to send'
        },
      },
    },
  };

  return pinpoint.sendUsersMessages(params).promise();
};
await sendPushNotification();