Spring XMPP and Google Cloud Messaging integration
I am using the spring xmp integration module to create a custom 3rd party server implementation connecting to GCM cloud services like in GCM Cloud Connection Server (XMPP) .
So far, I have successfully connected to the GCM server, however, when I post a message to the server, I get something like:
<message id="m-1366082849205" to="REGISTRATION_ID">
<body>{"hello":"world"}</body>
</message>
but I need to send something like this:
<message id="">
<gcm xmlns="google:mobile:data">
{
"to":"REGISTRATION_ID",
"message_id":"m-1366082849205"
"data":
{
"hello":"world",
}
}
</gcm>
</message>
I am using the latest SI, 4.0.4, this is my config in xml:
<int-xmpp:outbound-channel-adapter
id="gcmOutboundAdapter" channel="gcmOutboundNotificationChannel"
xmpp-connection="gcmConnection" auto-startup="true"/>
I am sending messages with a normal MessageBuilder like this:
Message<String> xmppOutboundMsg = MessageBuilder.withPayload(xmppPayload)
.setHeader(XmppHeaders.TO, REGISTRATION_ID)
.build();
where xmppPayload is a json string.
I need to customize / override the way xmpp composes the message, what is the best practice to achieve the result? Do I have to override the class that implements int-xmpp: outbound-adapter with a custom service activator, anyway, to customize how the xmpp message is generated?
Thanks for any help.
source to share
The message format is hardcoded in the Smack method Message.toXML()
(we are using the smack library at the bottom).
See @ Flow's answer.
Then the subclass ChatMessageSendingMessageHandler
, overriding handleMessageInternal()
- copies the code and installs the extension after posting.
The easiest way to set up your custom handler is probably to put it in chain
...
<chain input-channel="gcmOutboundNotificationChannel">
<bean class="foo.MyChatMessageSendingMessageHandler">
<constructor-arg ref="gcmConnection" />
</bean>
</chain>
Or you can wire it up as a top level bean and inject it into ConsumerEndpointFactoryBean
.
Feel free to open up the new Jira Issue feature and we'll look into adding an extension point to make it a little easier.
source to share
Until we introduce injection PackExtension
, you can overcome it with <transformer ref="">
, because it <int-xmpp:outbound-channel-adapter>
can receive org.jivesoftware.smack.packet.Message
as a message payload
:
<transformer ref="toGcmTransformer" output-channel="gcmOutboundNotificationChannel"/>
<int-xmpp:outbound-channel-adapter
id="gcmOutboundAdapter" channel="gcmOutboundNotificationChannel"
xmpp-connection="gcmConnection" auto-startup="true"/>
public class ToGcmTransformer extends AbstractTransformer {
protected Object doTransform(Message<String> message) throws Exception {
String to = message.getHeaders().get(XmppHeaders.TO, String.class);
xmppMessage = new org.jivesoftware.smack.packet.Message(to);
xmppMessage.setBody(message.getPayload());
xmppMessage.addPacketExtension(gcmPackExtension);
return xmppMessage;
}
}
Please raise a support question PackExtension
.
source to share
->
<int:chain input-channel="gcmOutboundNotificationChannel">
<!--<int:transformer ref="toGcmTransformer" output-channel="gcmOutboundNotificationChannel"/>-->
<bean class="com.payumoney.cardhash.service.MyMessageSendingMessageHandler">
<constructor-arg ref="gcmConnection" />
</bean>
</int:chain>
<int:transformer id="testTransformer" ref="toGcmTransformer" input-channel="gcmInboundNotificationChannel"
method="doTransform" output-channel="gcmOutboundNotificationChannel"/>
<!--<int:transformer ref="toGcmTransformer" output-channel="gcmOutboundNotificationChannel"/>-->
<int-xmpp:outbound-channel-adapter
id="gcmOutboundAdapter" channel="gcmOutboundNotificationChannel"
xmpp-connection="gcmConnection" auto-startup="true"/>
<int:chain input-channel="gcmInboundNotificationChannel">
<bean class="com.payumoney.cardhash.service.PayumoneyNotificationListeningEndpoint">
<constructor-arg ref="gcmConnection" />
<property name="outputChannel" ref="gcmOutboundNotificationChannel" />
</bean>
</int:chain>
<int-xmpp:inbound-channel-adapter
id="gcmInboundAdapter" channel="gcmInboundNotificationChannel"
xmpp-connection="gcmConnection" extract-payload="true" auto-startup="true" />
source to share