How to send Notification Message with gcm-server.jar - java

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!

Related

Receive SMS from Twilio

I am creating a program that asks the user something like "How are you doing today":
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Message message = Message.creator(new PhoneNumber("+000000"), // To
// number
new PhoneNumber("0000000"), // From number
"How are you doing today?" // SMS body
).create();
System.out.println(message.getSid());
And then then the program will listen for whatever response the user texts back from their phone.
Now, Twilio says this about receiving SMS:
You can associate that phone number with an SMS URL. When someone sends a text message to that phone number, Twilio makes an HTTP request to your URL with the body of the message and the sender's phone number. You can then respond to the SMS by returning a reply message in the HTTP response to Twilio.
Now, I understand that when the user texts back, Twilio makes an HTTP request to my program, like so:
However, in the tutorial, they create an HTTP tunnel using ngrok to allow the HTTP request to go through. My application is supposed to be able to run in anyone's computer without prior configuration. How would you recommend I achieve this?
I'm afraid that without exposing your application to the Net you won't be able to use that particular API.
What you can try to do instead is polling / fetching:
When you send an SMS or MMS message via the REST API, using the
<Message> verb in TwiML, or someone sends a message to one of your
Twilio numbers Twilio creates a Message instance resource. The
Messages list resource represents the set of messages sent from and
received by an account.
Retrieving sent and received messages from history can be achieved by
querying the Messages list resource.
A short example to start with is available here.

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.

GAE GCM Size of message

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.

How to make sure a SMS went through after sending it with Twilio

I am using the Twilio java wrapper provided on the website and started making some tests. I am able to send text messages that are successfully received. However, I would like to make sure that the messages have been sent successfully and that no problem has been encountered by Twilio (bad phone number or whatever reason).
I understand that when you make a REST request to Twilio to send a SMS, Twilio responds with the Status. How could I get this response?
Here is the explanation page I found: http://www.twilio.com/docs/howto/sms-notifications-and-alerts
If you specify a StatusCallback URL when you make the request to send an SMS, we will make a request to the callback URL you provided after the message has finished processing, with the parameters either SmsStatus=sent or SmsStatus=failed. You can use this information to do more processing on the SMS message. There's more information here: http://www.twilio.com/docs/api/rest/sending-sms#post-parameters-optional
Alternately, if you hang on to the SMS Message Sid, you should be able to query the API for the message and get the status in the response. So if the sid is SM123, making a GET request to https://api.twilio.com/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json should return a object with the status of the SMS Message.
I recall that the response comes to your url and can be matched up by an ID. In the REST post to SMSMessages you can specify a statuscallback url where Twilio will post a status message to your url.
When you receive that post to your site, you can record it or take any other action you need, such as retrying or using another mode of communication.
In 2020, with the Java SDK, you can now create a MessageFetcher with that SID and then call fetch to ask for the Message instance until its getStatus returns "delivered", "undelivered", or "failed".

Categories

Resources