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.
Related
We are using Twilio Programmable Messaging for sending SMS, in our Kotlin backend service with Twilio SDK.
Gradle dependency
implementation("com.twilio.sdk:twilio:7.51.0")
Also, we are tracking the delivery status of the sent SMS messages. For that, we need to set a status callback before sending an SMS.
val creator = Message.creator(to, from, textMessage)
.setStatusCallback(webhook.smsStatus)
Receiving message status
Our other service is set up for receiving a status callback.
#PostMapping(path = ["events"], consumes = [MediaType.APPLICATION_FORM_URLENCODED_VALUE])
#ResponseStatus(NO_CONTENT)
fun receiveSmsStatus(status: SmsStatus) {
communicationsService.processSmsStatus(status.MessageSid, status.MessageStatus)
}
Everything is working fine and we successfully receive and process SMS delivery statuses.
But every 100th status callback contains payload with nullable MessageSid and MessageStatus. At the same time, it contains payload with From and To phone numbers.
Why might this happen? And is it okay for MessageSid and MessageStatus to be NULL? I thought these fields are required.
I suggest opening a ticket with Twilio support via the Twilio Console (upper right corner under ? - Submit a ticket). If you can associate the outbound SMS SID returned when calling the /Messages resource with the associated statusCallback (say by matching the number the SMS was sent to since the MessageSID is null), that may help Twilio support identify the issue.
Screenshots in the ticket will also be useful for them.
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.
We currently face a strange issue when testing the GCM Service.
When we are sending a valid request (with a valid registration_id) then everything is fine and the notification is sent to the client app.
Then we wanted to test a kind of negative scenario, where we used a wrong registration_id (we just exchanged one letter) and then GCM returns 400 BAD REQUEST or 401 UNAUTHORIZED.
Based on the GCM Documentation we should get a "200+error:InvalidRegistration".
Edit 1: We only use the "to" and "content_available" fields in the request to GCM. JSON is fine, else it would not work with a valid
registration_id.
Edit 2: We found out that if you change a letter of the registration id manually, you will receive a Bad Request as response. This means that there might be some validations on GCM side to check if a registration_id is manipulated. If we use a registration_id from another app, we receive normal error responses from GCM.
Did anyone face this issue before?
Yes, you are right.
But;
If you send only one token with 'to' parameter, GCM returns 400.
if you try to send multiple tokens(some of them is valid and others not), GCM returns 200 and error list has 'InvalidRegistration' code.
As this scenario, I can say;
if use 'to', GCM returns 400.
if use 'registration_ids', GCM returns 200 and error list in response.
You can also check it with this library's test cases.
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
});
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".