首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Java将消息发送到特定设备

如何使用Java将消息发送到特定设备
EN

Stack Overflow用户
提问于 2018-01-04 23:57:32
回答 1查看 2.9K关注 0票数 1

我已经能够让我的移动安卓应用程序接收到从定位运动控制台(https://console.aws.amazon.com/pinpoint/home)生成的消息到特定的设备,方法是将该部分定位到只有该设备具有的自定义属性上。

精确运动配置

  • 移动推送通道
  • 标准战役
  • 使用自定义属性定义的段,保留0%。
  • 无声通知
  • 自定义JSON
  • 立即发射

现在,我想使用SDK在我的Java应用程序中实现这个特性,并针对设备的精确端点。

代码语言:javascript
复制
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);

"body“与我在Pinpoint活动控制台中输入的JSON相同。当我运行这个程序时,我会得到一个成功的DeliveryStatus,但是设备从来没有接收到这个消息。

代码语言:javascript
复制
{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尝试了这一点:

代码语言:javascript
复制
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的成功,但应用程序从来没有收到)。我尝试在AWS定位控制台中使用"Direct“消息,但它们似乎不支持相同的格式(强制操作和标题/消息,而不是带有自定义JSON的无声推送消息)。

我是不是搞错端点了?我如何将上述活动转化为一条信息?我也看到了一个sendUserMessages() API调用,但这似乎不是正确的(我找不到指定特定用户端点的位置)。

客户通过注册服务接收该活动:

代码语言:javascript
复制
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的直接信息是通过同样的活动方式发送的,还是我必须注册不同的服务来处理这些信息?

EN

回答 1

Stack Overflow用户

发布于 2018-01-05 19:56:02

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

代码语言:javascript
复制
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);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48105300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档