How to use Pinpoint send messages to specific devices using the Java AWS SDK for Pinpoint

耗尽温柔 提交于 2019-12-08 09:12:07

问题


I have been able to get my mobile Android app to receive messages generated from the Pinpoint Campaign console (https://console.aws.amazon.com/pinpoint/home) to a specific device by targeting the segment to a custom attribute that only that device has.

Pinpoint Campaign config

  • Mobile push channel
  • Standard campaign
  • Segment defined using custom attributes, holdout 0%
  • Silent notification
  • Custom JSON
  • Launch immediate

Now I would like to implement this feature in my Java app using the SDK APIs and target the device's Pinpoint endpoint.

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

The "body" is the same JSON I put in the Pinpoint Campaign console. When I run this, I get back a DeliveryStatus of SUCCESSFUL but the device never receives the message.

{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"}]},}}}

I have also tried this via the 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}}}"

with a similar result (get 200 status code and DeliveryStatus of SUCCESSFUL but the app never receives). I tried using the "Direct" message in the AWS Pinpoint console but they do not seem to support the same format (forces Action and Title/Message instead of silent push message with custom JSON).

Am I getting the endpoint incorrectly? How do I translate the above campaign into a message? I see there is a sendUserMessages() API call as well but that doesn't seem to be right one (I couldn't find where to specify the specific user endpoint)?

The client receives the campaign via the registered Service:

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

Do GCM direct messages get sent through the same campaign method or do I have to register a different service to process these?


回答1:


Found the solution based on the AWS CLI command I was able to run. Should have been using the "Data" element and not the "Body" and need to enable "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);


来源:https://stackoverflow.com/questions/48105300/how-to-use-pinpoint-send-messages-to-specific-devices-using-the-java-aws-sdk-for

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!