gpt4 book ai didi

android - 使 xmpp-connection (spring integration) 与 google gcm 一起工作的正确参数是什么?

转载 作者:行者123 更新时间:2023-11-30 02:19:05 26 4
gpt4 key购买 nike

我想在我的 spring 应用程序上设置 spring-integration-xmpp 以使其从 android 设备接收上游消息。我已经可以使用 http 将消息发送到 android 设备,但我无法设置 xmpp-connection bean,所以它给了我:

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

这是我的 spring 集成配置:

  <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****(我在星号中加上了)。

我错过了什么?

最佳答案

我已经将 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 配置中 Autowiring ,如下所示:

<!-- 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" />

最后一 block 是 androidMessageTransformer,它非常简单,就像 gcmXmppConnection bean 一样,它是根据 google 文档中的示例编码的。

@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;
}
}

这对我来说很可靠,虽然我主要处理出站方向,但从未检查过太多入站方向。

关于android - 使 xmpp-connection (spring integration) 与 google gcm 一起工作的正确参数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28854835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com