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

时间:2019-03-15 17:34:28

标签: android amazon-web-services react-native aws-amplify aws-pinpoint

我正在使用react-native,并通过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)
  });
});

3 个答案:

答案 0 :(得分:0)

使用Amazon Pinpoint,您无法将事务性消息作为推送通知发送。也就是说,您无法直接向特定收件人发送“推送”通知。

Amazon Pinpoint-推送通知支持通过创建广告系列和细分来向目标受众发送通知。

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

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

答案 1 :(得分:0)

我可以使用@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)
        })

    });

答案 2 :(得分:0)

这取决于您希望如何发送推送通知。我们创建了允许发送推送的 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();