什么是XMPP连接(Spring集成),使其与谷歌GCM工作权参数? [英] What are the right parameters for xmpp-connection (spring integration) to make it work with google gcm?

查看:1766
本文介绍了什么是XMPP连接(Spring集成),使其与谷歌GCM工作权参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置弹簧集成 - XMPP我的春天应用程序,使其从Android设备接收上游的消息。我可以使用http已经将消息发送到Android设备,但我不能建立XMPP连接豆所以它给了我:

I want to setup spring-integration-xmpp on my spring app to make it receive upstream messages from android devices. I can already send messages to the android device using http but I cannot set up the xmpp-connection bean so it gives me:

failed to connect to gcm-preprod.googleapis.com; nested exception is Connection failed. No response from server.:

这是我的春天一体化配置:

This is my spring integration configuration:

  <int:channel id="gcmOutboundNotificationChannel"/>

  <int-xmpp:xmpp-connection
    id="xmppConnection"
    user="${tracker.server.app.id}@gcm.googleapis.com"
    password="${tracker.auth.key}"
    host="gcm-preprod.googleapis.com"
    port="5236"
    subscription-mode="accept_all"/>

  <int-xmpp:outbound-channel-adapter
    id="gcmOutboundAdapter"
    xmpp-connection="xmppConnection"
    channel="gcmOutboundNotificationChannel"/>

tracker.server.app.id 是一个12位数字
tracker.auth.key AIzaSyBdfZ4oBaVuu07sjW5e9DnogeUF6NV **** (我放了星号)。

tracker.server.app.id is a 12 digit number and tracker.auth.key is like AIzaSyBdfZ4oBaVuu07sjW5e9DnogeUF6NV**** (I put in the asterisks).

我是什么失踪?

推荐答案

我已经配置了XMPP连接,这样的一个bean:

I have configured the xmpp connection as a bean like this:

@Configuration
public class GcmXmppConnection {

@Value("${gcm.senderID}")
private String username;

@Value("${gcm.apiKey}")
private String password;

@Value("${gcm.host}")
private String host;

@Value("${gcm.port}")
private int port;

@Bean(name="gcmConnection")
public XmppConnectionFactoryBean xmppConnectionFactoryBean(){

    ConnectionConfiguration configuration = new    ConnectionConfiguration(host, port);
    configuration.setSecurityMode(SecurityMode.enabled);
    configuration.setReconnectionAllowed(true);
    configuration.setRosterLoadedAtLogin(false);
    configuration.setSendPresence(false);
    configuration.setSocketFactory(SSLSocketFactory.getDefault());

//      configuration.setDebuggerEnabled(true);
    XmppConnectionFactoryBean connectionFactoryBean = new XmppConnectionFactoryBean(configuration);

    connectionFactoryBean.setUser(username);
    connectionFactoryBean.setPassword(password);

    return connectionFactoryBean;
}
}

的配置在这样的xml配置自动装配:

The configuration is autowired in the xml configuration like this:

<!-- Outbound messages to gcm -->
    <int:chain input-channel="androidNotificationOutputChannel">
    <int:transformer ref="androidMessageTransformer"></int:transformer>
    <int-xmpp:outbound-channel-adapter xmpp-connection="gcmConnection"/>
</int:chain>

<!-- Inbound messages from gcm -->
<int:channel id="gcmInboundNotificationChannel"/>
<int-xmpp:inbound-channel-adapter id="gcmInboundAdapter"
    channel="gcmInboundNotificationChannel" xmpp-connection="gcmConnection"
    extract-payload="true" auto-startup="true" />

最后一块是androidMessageTransformer,这是pretty简单,像gcmXmppConnection豆是谷歌以及文档中的例子codeD。

The last piece is androidMessageTransformer, it's pretty simple, like the gcmXmppConnection bean it was coded along the example in google documentation.

@MessageEndpoint
public class AndroidMessageTransformer extends AbstractTransformer {

public final static String DESTINATION_HEADER_KEY="push.destinationID";

private final static String MESSAGE_ID_FORMAT = "%s-%s";

@Value("${gcm.senderID}")
private String senderId;

@Autowired
ObjectMapper om;

@SuppressWarnings("unchecked")
@Override
protected Object doTransform(Message<?> msgIn) throws Exception {
    Map<String,String> data = (Map<String, String>) msgIn.getPayload();
    String registrationID = msgIn.getHeaders().get(DESTINATION_HEADER_KEY,String.class);
    Map<String, Object> gcmPayload = new HashMap<>();

    gcmPayload.put("to", registrationID);
    gcmPayload.put("message_id", String.format(Locale.ENGLISH, MESSAGE_ID_FORMAT, senderId, UUID.randomUUID().toString()));
    gcmPayload.put("data", data);

    String gcmJsonPayload = om.writeValueAsString(gcmPayload);

    org.jivesoftware.smack.packet.Message xmppMessage = new org.jivesoftware.smack.packet.Message();
    xmppMessage.addExtension(new GcmPacketExtension(gcmJsonPayload));

    return xmppMessage;
}
}

这工作可靠对我来说,虽然我已经大多与出方向努力,永不检查太多关于入站一边。

This works reliably for me, although I have mostly worked with the outbound direction, and never checked much about the inbound side.

这篇关于什么是XMPP连接(Spring集成),使其与谷歌GCM工作权参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆