To send a normal GCM, I can do
Sender sender = new Sender(apiKey);
Message message = new Message.Builder()
.addData("message", "this is the message")
.addData("other-parameter", "some value")
.build();
Result result = sender.send(message, registrationId, numOfRetries);
Will someone please show how I might send a topic message from Appengine (Java)
You can use this version of the Java GCM library. It will soon be merged into the master branch. It supports topic messaging. To send a topic message simply set the topic as the "to" field. Eg: /topics/mytopic
Sender sender = new Sender(apiKey);
sender.send(message, topic);
Related
I am setting up a Camel Route with ackMode=NONE meaning acknowlegements are not done automatically. How do I explicitly acknowledge the message in the route?
In my Camel Route definition I've set ackMode to NONE. According to the documentation, I should be able to manually acknowledge the message downstream:
https://github.com/apache/camel/blob/master/components/camel-google-pubsub/src/main/docs/google-pubsub-component.adoc
"AUTO = exchange gets ack’ed/nack’ed on completion. NONE = downstream process has to ack/nack explicitly"
However I cannot figure out how to send the ack.
from("google-pubsub:<project>:<subscription>?concurrentConsumers=1&maxMessagesPerPoll=1&ackMode=NONE")
.bean("processingBean");
My PubSub subscription has an acknowledgement deadline of 10 seconds and so my message keeps getting re-sent every 10 seconds due to ackMode=NONE. This is as expected. However I cannot find a way to manually acknowledge the message once processing is complete and stop the re-deliveries.
I was able to dig through the Camel components and figure out how it is done. First I created a GooglePubSubConnectionFactory bean:
#Bean
public GooglePubsubConnectionFactory googlePubsubConnectionFactory() {
GooglePubsubConnectionFactory connectionFactory = new GooglePubsubConnectionFactory();
connectionFactory.setCredentialsFileLocation(pubsubKey);
return connectionFactory;
}
Then I was able to reference the ack id of the message from the header:
#Header(GooglePubsubConstants.ACK_ID) String ackId
Then I used the following code to acknowledge the message:
List<String > ackIdList = new ArrayList<>();
ackIdList.add(ackId);
AcknowledgeRequest ackRequest = new AcknowledgeRequest().setAckIds(ackIdList);
Pubsub pubsub = googlePubsubConnectionFactory.getDefaultClient();
pubsub.projects().subscriptions().acknowledge("projects/<my project>/subscriptions/<my subscription>", ackRequest).execute();
I think it is best if you look how the Camel component does it with ackMode=AUTO. Have a look at this class (method acknowledge)
But why do you want to do this extra work? Camel is your fried to simplify integration by abstracting away low level code.
So when you use ackMode=AUTO Camel automatically commits your successfully processed messages (when the message has successfully passed the whole route) and rolls back your not processable messages.
I'm developing a winform program in which I need to send and receive SMS messages.
I have no problem sending SMS, but I don't know how to inform when a new message has been received via a GSM modem. I want to have an acknowledgement like an interrupt or event, when a new message has been received.
As far as I know I should work to at+CNMI or at+CNMA, but unfortunately I can't find an example or suitable reference for those. Furthermore, I need to know how to get delivery message or how to handle that?
AT+CNMI and AT+CNMA are standard AT commands defined in the spec 3GPP TS 27.005, available here.
For confirmation of successful delivery, see section 3.5.1 of this document which is the description for "Send message, +CMGS".
When you are defining new port in your connection to connect, you should get a data received trigger to it.
SerialPort port = new SerialPort();
//...
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
and in that trigger:
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
MessageBox.Show(sp.ReadExisting()); //Data received
}
Is there something like "browse" option (see section 2.4.3.3. browse for details) for amqp endpoint?
There is a "browse" component. to("browse:dummy"), but that will only make it possible to browse through messages that has passed this route. Handy in some cases, but not really as the JMS browse option.
What you can do is to use a BrowsableEndpoint to do a JMS browse (should work with AMQP as well, as it's based on a JMS client, haven't tried though).
You can't really receive a message and not delete it though, so you would need something else to trigger your browsing. Such as a timer or a trigger queue.
from("amqp:queue:trigger")
.process(new Processor(){
#Override
public void process(Exchange arg0) throws Exception {
BrowsableEndpoint browse = arg0.getContext().getEndpoint("amqp:queue:archive", BrowsableEndpoint.class);
List<Exchange> exchanges = browse.getExchanges();
System.out.println("Browsing queue: "+ browse.getEndpointUri() + " size: " + exchanges.size());
for (Exchange exchange : exchanges) {
String payload = exchange.getIn().getBody(String.class);
String msgId = exchange.getIn().getHeader("JMSMessageID", String.class);
System.out.println(msgId + "=" +payload);
}
}
});
You could do a pre-route to achive this trigger.
from("amqp:queue:processQueue")
.to("amqp:queue:archive")
.transform().constant("trigger msg")
.to("amqp:queue:trigger");
I figured out it eventually, I just thought that the option should be there on camel component options level, i.e. after connection string in form of e.g. "?mode=browse", but the option is actually to be set on amqp connection level - "my-queue; {mode: browse}". You can set camel component options adding "?option=value".
P.S. Setting the option on amqp connection level works for even-driven (default camel) routing, it does not work however for polling-driven one, see https://issues.apache.org/jira/browse/CAMEL-6784 for details.
Yes. In AMQP 0-10 there is an acquire mode option on message.subscribe which if set tonot-acquired results in a browsing subscription.In AMQP 1.0 the same is achieved by specifying a distribution mode of 'copy' for the source when establishing a subscriber link.
However, assuming you are accessing AMQP via a JMS client then as Petter says above, you should be able to use the browsing facility as defined by JMS I would imagine (I'm not familiar enough with camel to know how that is done).
I think I'm doing something wrong.
I want to send a XMPP message to my GTalk id but I don't want that the GTalk app receives the message so I'm changing the resource of the recipient JID.
My problem is that GTalk is receiving all the messages although thay have different resource.
My code:
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
// Parse incoming message
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message msg = xmpp.parseMessage(req);
JID jid = msg.getFromJid();
String body = msg.getBody();
String jidID = jid.getId().split("/")[0];
JID jid2 = new JID(jidID+"/myownresource453242352");
String response = jid2.getId() + " " + body;
// Send out response
msg = new MessageBuilder().withRecipientJids(jid2).withBody(response).build();
xmpp.sendMessage(msg);
}
The output:
Rafa Espillaque, 18:33 -
You shouldn't respond!
prueba-gae-gdx#appspot.com, 18:33 -
rafaespillaque#gmail.com/myownresource453242352 You shouldn't respond!
What's wrong?
UPDATE:
Now I'm sending messages to myapp#appspot.com/bot from an aSmack client and it is resending the message to me at my client.
The problem is GTalk for Gmail and GTalk for Android is registering all sent messages but they don't receive the app responses. Other clients don't show the messages I don't sent with them.
Will I be able to hide my messages to Gmail and Android?
My code:
SERVER
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
LOG.setLevel(Level.INFO);
// Parse incoming message
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message msg = xmpp.parseMessage(req);
LOG.info(msg.getStanza());
JID jid = msg.getFromJid();
String body = msg.getBody();
String response = "FullID: "+jid.getId()+" El mensaje recibido es: "+body;
// Send out response
msg = new MessageBuilder().
withRecipientJids(jid)
.withMessageType(MessageType.NORMAL)
.withBody(response)
.build();
xmpp.sendMessage(msg);
}
CLIENT:
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
XMPPConnection connection = new XMPPConnection(connectionConfiguration);
try {
Log.i("TAG","Trying to connect");
connection.connect();
Log.i("TAG","Connected");
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
Log.i("TAG","Trying to Log In");
connection.login("rafaespillaque#gmail.com",mypass, mires");
Log.i("TAG","Logged In");
} catch (XMPPException e) {
e.printStackTrace();
Log.i("TAG","Problem connecting or logging in");
}
//Creating chat object for processing friend chat
Chat chat = connection.getChatManager().createChat(Server, new MessageListener() {
//Overriding process message function of MessageListener Interface which will be
//called whenever a message is received
#Override
public void processMessage(Chat c, Message m) {
//Displaying message sent by friend
//System.out.println(friendId+ " : " + m.getBody());
Log.i("TAG", m.getBody());
message = m.getBody();
}
});
try {
Message out = new Message();
out.setBody("Definitivo22222222");
out.setType(Type.normal);
chat.sendMessage(out);
Log.i("TAG", "Mensaje enviado");
} catch (XMPPException e) {
Log.i("TAG", "No se envió el mensaje");
e.printStackTrace();
}
Last thing: I've seen in AppEngine Logs that the Stanza received from aSmack isn't of normal type but chat type.
Thanks for helping!!
Last-last thing: You can test what Gmail is doing by connecting from any client and Gmail at same time and talking from the client. Gmail is receiving your messages.
Thanks again.
Another thing:
My goalis use XMPP to communicate 2 clients of a game with their gmail account. Do you know an alternative?
See RFC 6120, section 10.5.4:
If the JID contained in the 'to' attribute is of the form
localpart#domainpart/resourcepart and the user exists but there is
no connected resource that exactly matches the full JID, the stanza
SHOULD be processed as if the JID were of the form
localpart#domainpart as described under Section 10.5.3.2.
If you send to an invalid resource, the server treats it as if you had sent it to the bare JID. On GoogleTalk, this goes to all non-negative priority resources.
I think that this is by design. IIRC GTalk routes all messages for a given JID to all connected resources of the JID. This is even true if the message has a full JID as to.
If you send the message with JID and resource (user#yoursever/reourcex) then the receiving server will route your request to the intended receiver. The receiving server will decide how to route the JID/resource.
A standard server will look for an exact match of the sent JID/resource. If found then it will behave as expected and the message will be delivered. If that fails on the other hand, it will send the message to the highest priority online resource. If that fails, it will store the message offline.
In the case of Google Talk this will happen: it will try to match the exact resource, and if that fails, broadcast it to all of the online resources. The same is also applied if no resource included.
UPDATE: The plot thickens. I changed my channel name and it is suddenly working (which means it wasn't a problem with my push service, since I'm getting the same HTTP response from the Microsoft Push Notification server).
To me, however, this is not a solution. How will I be able to test this and KNOW my users are getting their push notifications if I'm getting the same response when it's not working as I do when it is?
[ORIGINAL POST]
I've been trying to get push notifications sent to my Windows Phone 7 device, but I'm having very big problems that I can't find any answers for. I'll start with the c# code.
I set up push notifications using the following C# code.
private HttpNotificationChannel channel;
private static string PUSH_CHANNEL = "MySpecialPushChannel";
private Uri PushUri = null;
private bool IsPushRegistered = false;
public void StartPushSubscription()
{
try
{
channel = HttpNotificationChannel.Find(PUSH_CHANNEL);
}
catch
{}
if (channel != null)
{
PushUri = channel.ChannelUri;
if (!channel.IsShellTileBound)
channel.BindToShellTile();
}
else
{
channel = new HttpNotificationChannel(PUSH_CHANNEL);
channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(channel_ChannelUriUpdated);
channel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(channel_HttpNotificationReceived);
channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(channel_ErrorOccurred);
try
{
channel.Open();
channel.BindToShellTile();
}
catch (Exception err)
{
channel = null;
IsPushRegistered = false;
// Code to try again
}
}
}
void channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
PushUri = e.ChannelUri;
IsPushRegistered = true;
}
I'm following the standard WP7 push structure:
Find the HttpNotificationChannel (or start a new one)
Register event handler to get the push notification uri back
Open the channel
Bind to the tile
Handle the channel Uri (which we send to our service to await the happy day when we send the push notification
OK... so far so good. No errors, I get my Uri, send it to my service just fine. I pin my app to the start screen and my service sends a push request to the Uri (sending just the count so that I get a little push count number in the upper right hand corner). I get back an HTTP 200 status with the following:
DeviceConnectionStatus => Connected
NotificationStatus => Received
SubscriptionStatus => Active
And then... nothing. No push status shows up on my app. I've now tried it on my device, in the emulator, on another device, and with multiple servers and the result is always the same. Everything looks like it is working except for the fact that it doesn't work.
To me, however, this is not a solution. How will I be able to test this and KNOW my users are getting their push notifications if I'm getting the same response when it's not working as I do when it is?
The answer is, you can't. It's a limitation of how WP7 handles notifications.
For structured notifications like Tile and Toast, if you get the Connected/Active/Received/200 response, then you can know that MPNS accepted your notification request. However, this does not mean that you have sent a valid XML payload.
The component that handles parsing XML is the Push Client, the process running on the phone that accepts push notifications and deals them out to appropriate applications, displays the toast, etc.
If you have sent invalid XML, there is absolutely no indication that you've done so. At most, if you try to send the notification again to that same push channel URI, you'll get a 404 in response. Apparently getting an invalid XML payload for a specific application makes that application's push channel close, requiring you to go through the whole procedure again.
I've discovered this while debugging with our server team, and through trying to get the phone to display an alternate live tile. The only advice I can offer you is to quadruple-check your XML.
You will get errors in your error event handler for your push notification channel for Toast notifications that have invalid XML, since you are able to send/receive toast notifications while the application is active.
If anyone from Microsoft is reading this, PLEASE provide more thorough documentation on possible error states in the push notification system. We also need an event handler for Tile notifications, or at least allow us to receive tile notifications while the app is in the foreground and fire the notification channel error event so that we can be aware that our XML payload is invalid.
Especially if your web service isn't built with WCF, .NET, Azure, and whatever, working with Push Notifications on WP7 is like wandering blind.
Documentation for an exception message reading "InvalidOperationException(Failed to open channel)" should not read: "This exception is raised when the notification channel failed to open. Try opening the notification channel again." (reference)
are you getting the URL from each device? you need to get a URL from the push notification sevice for each device everytime your device connects,
when it does you need to find a way of retrieving the url from each client,
once you do that and your still not receiving push notifications then I would write to microsoft to see if they can see anything to do with the push notifications