【问题标题】:Send push notifications via AWS Pinpoint to specified User通过 AWS Pinpoint 向指定用户发送推送通知
【发布时间】:2019-03-15 17:34:28
【问题描述】:

我正在使用 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)
  });
});

【问题讨论】:

  • 你是如何在 react-native android 中获得设备令牌的?我有这个关于删除设备令牌的问题,请你看看:github.com/aws-amplify/amplify-js/issues/3080
  • @Lucky_girl 你必须在启动 .js (App.js) 文件中添加放大方法。我也遇到了这个问题,我将onRegister 方法移动到启动.js 文件,然后我就能够获得注册的令牌。
  • 我已经在componentDidMount()中把它移到了App.js中,但是仍然无法在那里获取token。你使用标准的 react-native 还是 expoKit?
  • 我使用的是 react-native init 而不是博览会。你添加了 firebase Api Key 吗?
  • 是的!您使用什么版本的 aws-amplify 和 aws pushnotifications?

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 您似乎有使用 aws pinpoint 的经验。你能帮忙看看我在下面链接的问题吗?谢谢。 stackoverflow.com/questions/55226785/…
    • @SamuelNde 我查过但找不到答案
    • 原始问题和解决方案有什么区别?除了在解决方案中添加了用户属性这一事实。
    • 我忘记在解决方案中添加Analytics.configure(aws_exports);。我会更新答案
    【解决方案3】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多