I used Paho MQTT client in Android to connect to IBM Bluemix quickstart IoT service. The connection part works well, but when I publish, the cloud application displays that I'm disconnected, but in the client I don't get exceptions.
I use this permission:
<uses-permission android:name="android.permission.INTERNET"/>
Connect:
String broker = "tcp://quickstart.messaging.internetofthings.ibmcloud.com:1883";
String clientId = "d:quickstart:iotqs-sensor:myDeviceID";
try {
client = new MqttClient(broker, clientId, null);
MqttConnectOptions connOpts = new MqttConnectOptions();
client.connect(connOpts);
} catch(MqttException me) {
Publish:
String topic = "iot-2/evt/iotsensor/fmt/jon";
String content = "{ \"d\" : { \"data\" : 5 } }";
try {
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(0);
client.publish(topic, message);
} catch(MqttException me) {
The strange thing is: this code worked yesterday.
What could be the problem?
The next step will be to connect and publish to my own Bluemix IoT service, but if I can't send messages to the demo, I can't hope for more.
UPDATE:
If I connect, and publish, the quickstart app shows that I'm disconnected, but the client still lets me publish for about 3 secs, after that I get an exception: 32104 (the client is not connected).
A Device must be registered before it can connect. Refer to quickstart documentation at https://docs.internetofthings.ibmcloud.com/messaging/devices.html#/ I don't see any devices registered under your org. Also, under the org it shows your email/ID as "expired". Please sign in at https://internetofthings.ibmcloud.com/#/ and add yourself as a permanent user under the access tab.
Related
I am attempting to implement a GCM-enabled Android application but I am having trouble authenticating with the CCS server from my 3rd-party-server.
import sleekxmpp as xmpp
SERVER = 'gcm.googleapis.com'
PORT = 5235
USERNAME = 'my-project-number'
PASSWORD = 'my-api-key'
def main():
client = xmpp.ClientXMPP(USERNAME + '#' + SERVER, PASSWORD)
if client.connect(address=(SERVER, PORT), use_ssl=True):
print('Connection established.')
print('Authenticated =', client.authenticated)
else:
print('Connection failed.')
if __name__ == "__main__":
main()
Output:
Connection established.
Authenticated = False
Process finished with exit code 0
Not sure as to why client.authenticated is always false when I know the credentials I have are the same ones on the project page in the Google Developer Console.
Since you haven't really supplied a specific error message from the logs I will post a quick troubleshoot here. I used it myself when I had problems.
Server Ip
Server IP: you have correctly set the IP of your 3rd party server in the console. It's where you configure the API key, so I bet this is ok.
Whitelisting
When you want to try out your project over xmpp you have to get it whitelisted, this is not so clear when reading the developer docs from google. Refer to this question for more explanation: Google CCS (GCM) - project not whitelisted . The link to get in line for whitelisting: https://services.google.com/fb/forms/gcm/
Http
If the above also did not grant results you might want to check out if everything works fine using HTTP json messages, for which your project does not has to be whitelisted. Since this method has been around for some time, there are some working libraries like this one: https://bitbucket.org/sardarnl/gcm-client
I need a little help understanding couple of things about gcm - server side.
I am working on instant messaging app with the example of server-side here http://developer.android.com/google/gcm/ccs.html
After I execute this code It connects but terminates after few seconds. I can guess that the reason it's because of the main() function.
public static void main(String[] args) throws Exception {
final long senderId = 1234567890L; // your GCM sender id
final String password = "Your API key";
SmackCcsClient ccsClient = new SmackCcsClient();
ccsClient.connect(senderId, password);
// Send a sample hello downstream message to a device.
String toRegId = "RegistrationIdOfTheTargetDevice";
String messageId = ccsClient.nextMessageId();
Map<String, String> payload = new HashMap<String, String>();
payload.put("Hello", "World");
payload.put("CCS", "Dummy Message");
payload.put("EmbeddedMessageId", messageId);
String collapseKey = "sample";
Long timeToLive = 10000L;
String message = createJsonMessage(toRegId, messageId, payload,
collapseKey, timeToLive, true);
ccsClient.sendDownstreamMessage(message);
}
Isn't supposed to be a 'while' statement which keeps the XMPP connection alive between the app-server to GCM server?
Isn't The purpose of the server-side in this case (instant messaging app) is to keep the connection alive with GCM server and to listen for incoming messages from clients.
messages like:
2.1 When client want to register to the app so I need to store it's details in a database.
2.2 When client want to send message to another client so the server-side app is in charge to forward the message to it's destination?
I have looked for couple of examples for implementing gcm server side using xmpp and all of them were with this kind of main function I mentioned above... Am I getting something wrong?
Yes, the XMPP connection must stay alive. Otherwise, your server won't be able to receive upstream (device to cloud) messages.
I can't say why all the examples you saw don't maintain an open connection. I guess they are simplified examples.
I am attempting to run gcm server using node-xmpp, but xmpp client does not seem to open at all and closes after timeout.
var xmpp = require('node-xmpp-client');
var options = {
type: 'client',
jid: 'fake-project-123#gcm.googleapis.com',
password: 'ApiKeyHere',
port: 5235,
host: 'gcm.googleapis.com',
legacySSL: true,
preferredSaslMechanism : 'PLAIN'
};
console.log("Creating XMPP Application");
var cl = new xmpp.Client(options);
cl.on('online', function()
{
console.log("XMPP Online");
});
Rest of the code was omitted. In the console, I never get to see "XMPP Online".
How do I check if xmpp is even connecting, and where it fails to open?
I got the same problem and found out that the Connection.startStream() was never called, although the socket was opened successfully.
Here's my pull request:
https://github.com/node-xmpp/node-xmpp-client/pull/61
Until it gets merged, you can use my fork, which should work for GCM:
https://github.com/Riplexus/node-xmpp-client
I followed this from gcm google groups and it worked for me.
And for timeouts you can try
xmppClient.connection.socket.setTimeout(0)
xmppClient.connection.socket.setKeepAlive(true, 10000)
Don't forget to whitelist your server ip in google console.
I have given up the hope of using node-xmpp and game smack client a try. Sadly it did not work, but I did get an error saying my project is not whitelisted. When project is whitelisted, it can receive messages from android devices, which is exactly what I need and is the sole reason why I went straight to CCS (XMPP). Without the whitelist, it is not possible to use CCS (XMPP) for sending the messages to android devices. In order to use HTTP method, the project does not need to be whitelisted, but has a limitation to being able to send messages only. I have signed up upstream GCM but have yet to receive response.
https://services.google.com/fb/forms/gcm/
I've recently been learning Android Development and I am trying to make a sample application which uses Google Cloud Messaging. My goal is to make a simple application which can receive Push notifications from a server. I've gotten the client side of the application to work by registering my device. Now I am trying to create the server side. However, I have absolutely no experience in setting up a server or programming on the server side. So I was hoping someone could point me in the right direction so that I could have a server sending the Push notifications. I have been following the tutorial on this link but I am stuck at the server implementation. I would greatly appreciate it if someone could point me in the right direction. Thanks!
Actually is more easier using Tomcat or AppEngine. See this tutorial in how to setup your GCM Server.
You need the device registration id to which you want to send the message on the server side you will need your API key, this is a JSP example :
http://yourdomain.com:8080/sendMessage.jsp?registrationID=kSADAS3242&messageToSend=Hello
String value = request.request.getParameter("messageToSend");
String registrationId = request.getParameter("registrationID");
Sender sender = new Sender("YOUR API KEY");
Message message = new Message.Builder().addData("FLAG","SERVE").addData("MSG", value).build();
Result result = sender.send(message, registrationId, 5);
On your client device should expect :
#Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Got a message from Google Cloud Messaging !!");
String tag = intent.getExtras().getString("FLAG");
String message = intent.getExtras().getString("MSG");
Log.i(TAG, tag + " : " + message);
}
This should print "SERVE : Hello"
If you have used PHP you should be familiar with xampp or similar software.
If not, all you have to do is download and install it, start the services and on your browser go to:
http://localhost/xampp
to test if it's properly installed.
If you can see the Xampp page you can start running scripts php from xampp/htdocs and run them like this:
http://localhost/yourscript.php
Try a simple hello world:
<?php
echo 'hello world';
?>
After that you should be ready to start following this tutorial or any tutorial in google just typing gcm php tutorial
I found that php is the most simple way for me to configure the server side for GCM, hope you find it useful...
I use the example provided by goole and I built an client side application to get notifications and a server side, both applications (code) is those provided by google.
I run the application on the client side and I get the registration_id. some huge string, in this format which is below:
APA91bEgguwt98xLbivrXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXLxDalNoEktCSVbOkT0-uFH2FaRnfpkRME2tzuvr0BycMNEhX_Ix1PV_XXXXXXXXXXXXXXXXXXXXXN5hcseY_wA.
This proves me that the client application works. Now the server (java, google example):
public static void main(String[] args) {
Sender sender = new Sender("AIzaSXXXXXXXXXX_XXXXXXXXXXXXXXXrQOnoGZw");
Message message = new Message.Builder()
.collapseKey("1")
.timeToLive(3)
.delayWhileIdle(true)
.addData("message",
"this text will be seen in notification bar!!")
.build();
try {
Result result = sender.send(message, "APA91bEgguwt98xLbivrXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXLxDalNoEktCSVbOkT0-uFH2FaRnfpkRME2tzuvr0BycMNEhX_Ix1PV_XXXXXXXXXXXXXXXXXXXXXN5hcseY_wA", 3);
System.out.println(result.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is the server. When I run the server I got as result something like this:
[ messageId=0:1341907115903155%921c249a00000031 ]
but no message on the client and I don't understand why. What is the meaning of the response I got from server? Is it the success response?
I need help to understand what is wrong?
Please review their comment
http://developer.android.com/guide/google/gcm/gcm.html
Note: If your organization has a firewall that restricts the traffic to or from the Internet, you'll need to configure it to allow connectivity with GCM. The ports to open are: 5228, 5229, and 5230. GCM typically only uses 5228, but it sometimes uses 5229 and 5230. GCM doesn't provide specific IPs. It changes IPs frequently.
According to the GCM Documentation:
When a 3rd-party server posts a message to GCM and receives a message ID back, it does not mean that the message was already delivered to the device. Rather, it means that it was accepted for delivery. What happens to the message after it is accepted depends on many factors.
In the best-case scenario, if the device is connected to GCM, the screen is on, and there are no throttling restrictions (see Throttling), the message will be delivered right away.
Remove your internet firewall if you are using it and try again
Please go through this
If your facing delay notification or message problem in GCM then try this solution.
I know this is not proper solution but it's WORKS Install this app it's really work for me HERE