I want to send customized message through gcm like a question with options and get the reply of that question from the receiver.
In simple terms I want to make a voting app which uses GCM service for asking questions and getting response.
Have you checked Sending Upstream Messages? A code Block from the source Link:
try {
Bundle data = new Bundle();
data.putString("my_message", "Hello World");
data.putString("my_action","SAY_HELLO");
String id = Integer.toString(msgId.incrementAndGet());
gcm.send(SENDER_ID + "#gcm.googleapis.com", id, data);
msg = "Sent message";
}
catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
You can add your Votes in data. Also you might wanna send your GCM token, that you receive in RegistrationIntentService.onHandleIntent(). This token can then be used by the server to uniquely identify you and send messages to you directly via GCM.
Check Downstream Messaging Via HTTP Post, your JSON will look something like this.
https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
Where "to" has the device's token.
Related
I am using ngWebsocket for listening user actions and update all users page according to current action not just page that who send action.
And I make a end point in java who catch all actions and send message all open sessions. but when i testing, end point find sessions and send message to all of them but message just come to person who send action.
my java code like
#OnMessage
public String onMessage(Session session, String message) { Gson gson = new Gson();
SocketMessage sm = gson.fromJson(message, new SocketMessage().getClass());
if (sm.getEvent().equals("teklif")) {
Set<Session> openSessions = session.getOpenSessions();
for (Session openSession : openSessions) {
try {
openSession.getBasicRemote().sendText("{\"event\":\"teklif\",\"data\":" + sm.getData() + "}");
} catch (Exception ioe) {
System.out.println(ioe.getMessage());
}
}
}
return message;
}`
when i debug Set<Session> openSessions = session.getOpenSessions(); it show me two session and send message to all remote. And I listen in my controller
$rootScope.ws.$on('teklif', function (data) { console.log(data);
});
it is shown only person who emit the message
note : I send message like this -->$rootScope.ws.$emit('teklif', data.content);
How can I make this socket that all user listen all actions ?
Thanks in advance.
Your are using Session.getOpenSessions(). The Javadoc states:
Return a copy of the Set of all the open web socket sessions that
represent connections to the same endpoint to which this session
represents a connection. The Set includes the session this method is
called on. These sessions may not still be open at any point after the
return of this method. For example, iterating over the set at a later
time may yield one or more closed sessions. Developers should use
session.isOpen() to check.
So it does not give you the set of all client sessions connected to your endpoint.
Instead you need to keep track of all session connected to your endpoint for yourself and iterate over that set. Here is an example.
I found my problem what is it .
#OnMessage
public String onMessage(Session session, String message) {
Gson gson = new Gson();
SocketMessage sm = gson.fromJson(message, new SocketMessage().getClass());
if (sm.getEvent().equals("teklif")) {
//SoncketTestMessage fromJson = gson.fromJson(test.getData(), SoncketTestMessage.class);
Set<Session> openSessions = session.getOpenSessions();
for (Session openSession : openSessions) {
try {
SocketResponse rsp = new SocketResponse();
rsp.setEvent("teklif");
rsp.setData(gson.toJson(sm.getData()));
//openSession.getBasicRemote().sendText("{\"event\":\"teklif\",\"data\":" + sm.getData() + "}");
openSession.getBasicRemote().sendText(gson.toJson(rsp, SocketResponse.class));
} catch (Exception ioe) {
System.out.println(ioe.getMessage());
}
}
}
return null;
}
i made a mistake at
openSession.getBasicRemote().sendText("{\"event\":\"teklif\",\"data\":" + sm.getData() + "}");
i just changed sm.getData and send right json format then it send to all user.
It send just to owner before because of that function return message and it is at right format and only owner get the return. Now all user are getting the message.
I have get an Internal server exception for getting mail information while using lutung's java API of mandrill. Here is my code.
public MandrillMessageInfo getMessageInfo(String id) {
MandrillApi mandrillApi = new MandrillApi("Your api key");
MandrillMessageInfo info = null;
try {
info = mandrillApi.messages().info(id);
log.debug("Message status-> Email {}, state: {}, timeOfSent: {} ", info.getEmail() ,info.getState(), TimeUtil.getLocalTimeString(info.getTs()));
} catch (Exception e) {
log.debug("Exception occurs while getting message info for id: {}, exception is: {} ", id, e.getMessage());
throw new MailServiceException(ErrorCodes.ERROR_INTERNAL_SERVER_ERROR, ErrorCodes.ERROR_MESSAGE_INTERNAL_SERVER_ERROR);
}
return info;
}
I see nothing wrong in your code. If your API key is right, the only reason this request will return an error is if you give a wrong id. You can verify that by issuing a messages/info api request directly on mandrill website using "Try It" button https://mandrillapp.com/api/docs/messages.JSON.html#method=info and pass the same message id, that you are giving via the program call.
Hope this helps,
I want to send a message to all active clients.
#OnMessage
public void onMessage(String message, Session session) {
switch (message) {
case "latencyEqualize":
for (Session otherSession : session.getOpenSessions()) {
RemoteEndpoint.Basic other = otherSession.getBasicRemote();
String data = "Max latency = "
+ LatencyEqualizer.getMaxLatency(latencies);
try {
other.sendText(data);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
break;
default:
RemoteEndpoint.Basic other = session.getBasicRemote();
try {
other.sendText(message);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Something is wrong with this code. When i send message "latencyEqualize" from the first client the server answers only to the same client. Other clients don't receive message "Max latency = 15". But when the second client sends to server any message, he recieves back "Max latency = 15". And all future calls to server return the message from previous call.
Is there a way to avoid this. I want all clients get "Max latency" message when one of them send "latencyEqualize" message to the server.
The reason why only one client receives your message is that session variable contains connection only of that client who sent you message.
To send your message to all clients, store their connections in some collection (for example, ArrayList<Session>) in onOpen() method, and then iterate though that collection to get connections of all of your clients
I've noticed errors in my Google App Engine Log while parsing messages. The stack trace was unhelpful to diagnose the problem, so I wrote a small message dumper inspired by Google's implementation of InboundMessageParser.
public class ChatRequestParser extends HttpRequestParser {
public static Map<String, String> parseMessage(HttpServletRequest request)
throws IOException {
try {
Map<String, String> message = new HashMap<String, String>();
MimeMultipart multipart = parseMultipartRequest(request);
int parts = multipart.getCount();
for (int i = 0; i < parts; i++) {
BodyPart part = multipart.getBodyPart(i);
String fieldName = getFieldName(part);
String fieldValue = getTextContent(part);
message.put(fieldName, fieldValue);
}
return message;
} catch (MessagingException ex) {
throw new IOException("Could not parse incoming request.", ex);
}
}
}
I found out that Google+ sends two messages for each message, only one of which contains a body (Gmail Talk client sends only one message).
Here is the first message, without a body:
{to=xxx#appspot.com, stanza=<message to="xxx#appspot.com" type="chat"
from="yyy#gmail.com/TalkGadgetD9F45A83" xmlns="jabber:client">
<cha:composing xmlns:cha="http://jabber.org/protocol/chatstates"/>
<nos:x value="disabled" xmlns:nos="google:nosave"/>
<arc:record otr="false" xmlns:arc="http://jabber.org/protocol/archive"/>
</message>, from=yyy#gmail.com/TalkGadgetD9F45A83}
And the second one is (my payload is many asterisks, mails changed):
{to=xxx#appspot.com, body=**********************************,
stanza=<message to="xxx#appspot.com" type="chat"
id="7279D79D0.17809585028724073_:sl" from="yyy#gmail.com/TalkGadgetD9F45A83"
xmlns="jabber:client"><body>**********************************</body>
<cha:active xmlns:cha="http://jabber.org/protocol/chatstates"/>
<nos:x value="disabled" xmlns:nos="google:nosave"/><arc:record otr="false"
xmlns:arc="http://jabber.org/protocol/archive"/></message>,
from=yyy#gmail.com/TalkGadgetD9F45A83}
Since the first message doesn't have a body calling parseMessage() on XMPPService throws exception. Has anyone noticed this problem?
Now I am catching the IllegalArgumentException and throwing away meaningless messages, but the real problem is, that the reply to the valid message doesn't arrive back to Google+ client, while works perfectly with Gmail and also with my Jabber client on Linux.
I've filed issue 6467.
I can reproduce the crash when no body is set and parseMessage is called, and I'm fixing it. Thanks for finding it!
However, I can't repro the "send reply doesn't work" bug. I have code like this:
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message message = xmpp.parseMessage(req);
Message reply = new MessageBuilder().withFromJid(message.getRecipientJids()[0])
.withRecipientJids(message.getFromJid())
.withBody("Back at you!")
.build();
xmpp.sendMessage(reply);
And I receive the reply both in Google+ and in Gmail. What are you doing differently?
I am using "simplewire" library (for Java) which should be able to send SMS from PC to mobile. However, I am seeting the following error:
Message was not sent! Error Code: 420
Error Description: Invalid Subscriber
ID or Subscriber Password.
Here is my code sample
import com.simplewire.sms.*;
public class send_text
{
public static void main(String[] args) throws Exception
{
SMS sms = new SMS();
// Subscriber Settings
sms.setSubscriberID("123-456-789-12345");
sms.setSubscriberPassword("1234");
// Message Settings
sms.setMsgPin("+11005101234");
sms.setMsgFrom("Demo");
sms.setMsgCallback("+11005551212");
sms.setMsgText("Hello World From Simplewire!");
System.out.println("Sending message to Simplewire...");
// Send Message
sms.msgSend();
// Check For Errors
if(sms.isSuccess())
{
System.out.println("Message was sent!");
}
else
{
System.out.println("Message was not sent!");
System.out.println("Error Code: " + sms.getErrorCode());
System.out.println("Error Description: " + sms.getErrorDesc());
System.out.println("Error Resolution: " + sms.getErrorResolution() + "\n");
}
}
}
I want to know how to get Subscriber ID and Subscriber Password? Do I need an account for that?.
From the Simplewire Java SMS SDK Manual
The subscriber ID is an ID number
provided to paid subscribers that
gives access to all of Simplewire’s
resources. The appropriate password
must also be set.
SimpleWire is now OpenMarket
You would need to setup an account with OpenMarket to use their SDK.
They offer two types of accounts:
Demo: which is where you could test your code on a short code that is for testing
Commercial: This is where you would pay for a Short Code and access to their platform/service