GAE GCM Size of message - java

In the GCM documentation (http://developer.android.com/google/gcm/gcm.html) it states that there's a 4kb payload limit per message. I am struggling to figure out how long my messages are. They're currently simply defined in a String and I use the packages
com.google.android.gcm.server.Message
com.google.android.gcm.server.Sender
to send the messages. The messages sent and received are fine. I'm just wondering if there was a way to see how many bytes each message is at the moment to see how much more I can add. I tried printing out the default encoding using Charset.defaultCharset() but I'm not sure if that's the actual encoding. It returned US-Ascii.
Currently the sending of messages goes something like:
Message message;
message = new Message.Builder()
.addData("MESSAGE_TYPE", "version1")
.addData("PERSONNAME", "john")
.addData("PHONENUMBER", "5551234567")
.build();
Sender sender = new Sender(API_KEY);
try{
MulticastResult result = sender.send(message, registrationIds, 5);
}
Is there a way to determine how many bytes the message actually is? Thanks.

Related

Java JMX Message None UTF Body to String

I am subscribed to AMQ. I am trying to receive and store specific messages as they come across the wire. Some of the message that I am receiving have garbage values in body and when I perform the toString() after casting to a TextMessage the text is null. The error that I received is a UTF exception. I fully expect the message to fail the .getText(). Is there a way in Message to get the text without having to validate the xml or validate the that it is UTF?
Thanks

How to send Notification Message with gcm-server.jar

I am using the gcm-server.jar to send gcm Messages from Server because its easy to use. (http://www.java2s.com/Code/Jar/g/Downloadgcmserverjar.htm).
Messages are sent with this code. This works fine:
Message msg = new Message.Builder().addData("message", message).build();
Sender sender = new Sender();
Result result = sender.send(msg, token, 5);
...
How can I send a GCM Message with Notification Payload like in this JSON:
{"to":"token" ,
"notification":{
"sound":"default",
"badge":"1",
"title":"this is the title",
"body":"this is the body"}}
You need to have a server set up where you parse these messages in JSON format so that GCM can process it accordingly.
Usually this depends on what server technology you are using. Also, you might want to check the validity of the library you referenced as GCM framework has been updated substantially.
Here's a good place to start.
And another good tutorial here. (although this one is older too but gives you understanding of server side implementation)
Hope this helps!

Web Socket - Spring : Confirm of message received

I am sending a message through WebSocket with Spring from Tomcat Server to a SockJSClient with the method:
WebSocketSession.sendMessage(WebSocketMessage<?> message)
and I would like to know when the message has been received (eventually with complementary information, for example whether the logic on client successfully processed), then go for next message.
This is an Activity diagram that explains the use case.
How can I receive confirmation of reception or result from client?
As Erwin pointed, you can adopt some higher protocol providing such feature like STOMP. However, if you are afraid to adopt it only for that feature, you can implement that feature by yourself.
The first thing is to give each message id to identify each message, type to recognize the purpose of each message, data to transport a message's content and reply which is a flag to see whether or not ACK is required and to use a format like JSON to serialize/deserialize an object containing these data into/from WebSocket message.
When sending a message, it creates an object by issuing a new id for that message, setting type to message and data to given message and reply to true if ACK is required or false if not. And it serializes it into JSON and sends it as a WebSocket message. - https://github.com/cettia/cettia-protocol/blob/1.0.0-Alpha1/lib/server.js#L88-L110
When receiving a message, it deserializes JSON to the above object. If reply is true, it sends a special message whose type is reply setting data to id of that message. Then, the counterpart can confirm that its counterpart has received a message whose id is id. - https://github.com/cettia/cettia-protocol/blob/1.0.0-Alpha1/lib/server.js#L46-L76
The above links point similar implementation in Cettia which is a real-time web application framework I wrote. Though that implementation is a little bit complex as it is designed to allow for user to handle callbacks with result, you are likely to get the basic idea.
API implemented by that link looks like the following.
A server or client which requires a result of event processing.
// Using Java server with lambda
socket.send("foo", "bar", result -> /* resolved */, reason -> /* rejected */);
The corresponding client or server which has a responsibility to submit the result.
// Using JavaScript client with arrow functions
socket.on("foo", (data, reply) => {
// data is 'bar'
// 'reply.resolve(result)' if it successes
// 'reply.reject(reason)' if it fails
});

InvalidRegistration when sending message to device via GCM

I am trying to send a GCM message to my device, and for some reason, on the server, the getErrorCodeName() is returning InvalidRegisration.
I basically implemented the example from google, and registered, and sent the registration ID to the log, and wholesale copied it from the log to the code on the server where I am trying to do the send.
Any idea on what could be wrong?
Result result = sender.send(message,"foo", 1);
System.out.println("Message sent: "+result.getErrorCodeName());
I have double checked to see that logcat is not truncating the value being printed out, and it isn't. I logged the length of the registration id and matched it with the length of the string/regid I'm using on the server when I am sending the notification.
Not sure what's going on.
The send() method has to follow this format:
send (Message message, String registrationId, int retries)
Looking at your snippet of code, you will need to revise the second parameter.
If you had just replaced the actual registration ID for privacy reasons, I would suggest checking that you are sending the registration ID completely. As mentioned here, it could have been truncated or altered in transit from your client device to your server.

Twilio msg status is always remains Sent

Messages are not delivered to numbers but Twilio is charging me and giving me status sent only with my code.
private val client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
def sendSMS(smsTo: String) = {
val params = Map("To" -> smsTo, "From" -> twilioNumber, "Body" -> "status?")
val messageFactory = client.getAccount.getSmsFactory
messageFactory.create(params)
}
But when I am using API Explorer its giving status delivered.
Is it my code's fault or Twilio?
I want my SMS delivered to users.
Twilio Evangelist here here.
When you send the SMS message using the REST API (the Java/Scala helper library here) you are getting the status sent because Twilio has sent the message to the carrier. The carrier will typically update Twilio to say the message was delivered.
There should be nothing wrong with your code at all. What you can do is capture the return value from:
messageFactory.create(params)
Which will contain a message ID (messageSID below). You can then fetch this message from the server to get an update on its status with something like:
var params = Map("SID" -> messageSID)
client.getAccount.getSmsMessage(params)
The full list of status are here. But it looks like everything you have should be spot on. Hope this helps.

Categories

Resources