如何使用Java AWS SDK for Pinpoint将Pinpoint发送到特定设备

时间:2018-01-04 23:57:33

标签: aws-pinpoint

我已经能够通过将细分受众群定位到仅包含该设备的自定义属性,将我的移动Android应用程序接收从Pinpoint广告系列控制台(https://console.aws.amazon.com/pinpoint/home)生成的消息到特定设备。

精确定位广告系列配置

  • 移动推送频道
  • 标准广告系列
  • 使用自定义属性定义的细分,保留0%
  • 无声通知
  • 自定义JSON
  • 立即发布

现在,我想在我的Java应用程序中使用SDK API实现此功能,并定位设备的Pinpoint端点。

GetEndpointRequest getEndpointRequest = new GetEndpointRequest()
   .withApplicationId(appId)
   .withEndpointId(endpointId);
GetEndpointResult endpointResult = getAmazonPinpointClient().getEndpoint(getEndpointRequest);

DirectMessageConfiguration directMessageConfiguration =
  new DirectMessageConfiguration().withGCMMessage(new GCMMessage().withBody(body).withSilentPush(true).withAction(Action.OPEN_APP));
AddressConfiguration addressConfiguration = new AddressConfiguration().withChannelType(ChannelType.GCM);

MessageRequest messageRequest = new MessageRequest().withMessageConfiguration(directMessageConfiguration)
   .addAddressesEntry(endpointResponse.getAddress(), addressConfiguration);

SendMessagesRequest sendMessagesRequest = new SendMessagesRequest()
   .withApplicationId(appId)
   .withMessageRequest(messageRequest);

"身体"与我在Pinpoint Campaign控制台中放置的JSON相同。当我运行它时,我得到了一个成功的DeliveryStatus,但设备从未收到消息。

{ApplicationId: MY_APP_ID,Result: {clrVUcv-AwA:APA91bHGXkxpDJiw5kOMROA2XTJXuKreMklq9jemHO_KGYTIw6w84Fw9zLv9waMgLgha61IR-kZxgmrnFu-OGp8l6WFgp4Wolh4oOvZwMobGYNgzivv3bGIK83t-e4hiLx1TTaEIeRdQ={DeliveryStatus: SUCCESSFUL,StatusCode: 200,StatusMessage: {"multicast_id":4803589342422496921,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1515105369948916%c551fa42f9fd7ecd"}]},}}}

我也通过AWS CLI尝试了这个:

aws pinpoint send-messages --application-id MY_APP_ID --message-request "{\"Addresses\":{\"clrVUcv-AwA:APA91bHGXkxpDJiw5kOMROA2XTJXuKreMklq9jemHO_KGYTIw6w84Fw9zLv9waMgLgha61IR-kZxgmrnFu-OGp8l6WFgp4Wolh4oOvZwMobGYNgzivv3bGIK83t-e4hiLx1TTaEIeRdQ\":{\"ChannelType\":\"GCM\"}},\"MessageConfiguration\":{\"GCMMessage\":{\"Body\":\"{\\\"message\\\":\\\"stuff\\\"}\",\"SilentPush\":true}}}"

具有类似的结果(获得200状态代码和成功的DeliveryStatus但应用程序永远不会收到)。我尝试使用" Direct" AWS Pinpoint控制台中的消息但它们似乎不支持相同的格式(强制使用Action和Title / Message而不是使用自定义JSON的静默推送消息)。

我是否错误地获取了端点?如何将上述广告系列转换为消息?我看到有一个sendUserMessages()API调用,但它似乎是正确的(我无法找到指定特定用户端点的位置)?

客户通过注册的服务接收广告系列:

public class PushListenerService extends GcmListenerService {

@Override
public void onMessageReceived(final String from, final Bundle data) {
    AWSMobileClient.initializeMobileClientIfNecessary(this.getApplicationContext());
    final NotificationClient notificationClient = AWSMobileClient.defaultMobileClient()
            .getPinpointManager().getNotificationClient();

    NotificationClient.CampaignPushResult pushResult =
            notificationClient.handleGCMCampaignPush(from, data, this.getClass());


    Log.e(LOG_TAG, " onMessageReceived - got messages"  + data);

通过相同的广告系列方法发送GCM直接消息,还是必须注册其他服务来处理这些消息?

1 个答案:

答案 0 :(得分:2)

根据我能够运行的AWS CLI命令找到解决方案。应该一直使用"数据"元素而不是" Body"并且需要启用" SilentPush"。

EndpointResponse endpointResponse = getPinpointEndpointResponse(appId, pinpointEndpointId);

Map<String, String> data = new HashMap<>();
// construct data here, currently only supports Map<String, String>
// why not HashMap<String, Object> so it can support full JSON????

DirectMessageConfiguration directMessageConfiguration =
    new DirectMessageConfiguration().withGCMMessage(new GCMMessage().withData(data).withSilentPush(true));

AddressConfiguration addressConfiguration = new AddressConfiguration().withChannelType(ChannelType.GCM);

MessageRequest messageRequest = new MessageRequest().withMessageConfiguration(directMessageConfiguration)
    .addAddressesEntry(endpointResponse.getAddress(), addressConfiguration);

SendMessagesRequest sendMessagesRequest = new SendMessagesRequest()
    .withApplicationId(appId)
    .withMessageRequest(messageRequest);