我想设置弹簧集成,XMPP在我的春天应用程序,使其从Android设备接收上游消息。 我可以使用HTTP已经将消息发送到Android设备,但我不能建立XMPP连接豆所以它给了我:
failed to connect to gcm-preprod.googleapis.com; nested exception is Connection failed. No response from server.:
这是我的春天一体化配置:
<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****
(I放于星号)。
我在想什么?
我已经配置了XMPP连接,这样一个bean:
@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配置:
<!-- 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,这是很简单的,就像gcmXmppConnection豆它是沿着谷歌文档中的例子编码。
@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;
}
}
该工作可靠对我来说,虽然我大多是用出方向努力,永不检查太多关于入站侧。
我认为你缺少auto-startup="true"
在<int-xmpp:outbound-channel-adapter
标签。 请参阅下面的答案可能为你工作。
文章来源: What are the right parameters for xmpp-connection (spring integration) to make it work with google gcm?