Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring integration XMPP and Google Cloud Messaging

I'm using the spring integration xmpp module to write a custom implementation of a 3rd party Server connecting to GCM cloud services, as in GCM Cloud Connection Server (XMPP).

So far I've successfully connected to the GCM server, however when I send a message to the server I end up with 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 use the latest SI version, 4.0.4, this is my configuration in the xml:

<int-xmpp:outbound-channel-adapter
    id="gcmOutboundAdapter" channel="gcmOutboundNotificationChannel"
    xmpp-connection="gcmConnection" auto-startup="true"/>

I'm sending messages with the usual MessageBuilder like this:

Message<String> xmppOutboundMsg = MessageBuilder.withPayload(xmppPayload)
        .setHeader(XmppHeaders.TO, REGISTRATION_ID)
        .build();

where xmppPayload is a json string.

I need to configure/override the way the xmpp message is composed, what is the best practice to achieve the result? Should I override the class implementing int-xmpp:outbound-channel-adapter with a custom service activator, is there anyway to configure the way the xmpp message is composed?

Thanks for any help.

like image 999
LittleSquinky Avatar asked Mar 13 '26 09:03

LittleSquinky


2 Answers

<gcm xmlns="google:mobile:data"> is a extended content element (see RFC 6120 8.4), which is modelled as PacketExtension in Smack. Do not subclass message, instead create a GCMPacketExtension class and add a instance of it to your message

message.addPacketExtension(gcmPackExtension)
like image 96
Flow Avatar answered Mar 15 '26 00:03

Flow


The format of the message is hard-coded in the Smack Message.toXML() method (we use the smack library underneath).

See @Flow's answer.

Then, subclass ChatMessageSendingMessageHandler, overriding handleMessageInternal() - pretty much copy the code and set the extension after the message is created.

The easiest way to configure your custom handler is probably to put it in a 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 a ConsumerEndpointFactoryBean.

Feel free to open a New Feature JIRA Issue and we'll consider adding an extension point to make this a bit easier.

like image 36
Gary Russell Avatar answered Mar 14 '26 23:03

Gary Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!