I am working on a IoT project where i'm using mosquitto broker to get messages to my android app. But when i receive the published messages on my app, they appear to be in a weird format. how can i get the data in correct format. Do i need to change something in mosquitto configuration.
The data sent: "hello"
Received on app: [B#2df51a89
this is what i use to get the text in onMessageReceived(topic,message)
message.getPayload().toString();
Any help is appreciated.
[UPDATE] - When i publish data FROM mobile app, the data is received perfectly at the subscriber. Vice versa doesn't work.
You've printed out the object representation of the byte array (message payload). The correct thing to have done is to create a new String from the byte array using a known encoding (probably UTF8)
String payload = new String(message.getPayload(), "utf8")
Then print the payload String
Related
I am using Apns-Node.js for sending push notifications to my iOS device and GCM-Node.js for my Android device.
Currently I have a payload sent to APNS which contains special characters like "ü" in the payload. This gets displayed in the same format and doesn't get truncated or modified by APNS for any reason.
But my Android app has this issue where GCM truncates the special character in the payload unless it is encoded using URL encoding.
"türken" -> gets truncated to "trken" unless it is encoded like "t%C3%BCrken" in GCM only
In an effort to keep both platforms uniform, can I send the encrypted payload to APNS as well? And if I do so, will APNS understand the UTF-8 format and send the payload with special character to the device? My expectation is that APNS should understand the encoded format "t%C3%BCrken" and send "türken" to the device. Will this work?
Based on your explanation, APNS properly supports UTF-8. This means it will deliver the payload as is.
This means if you send "t%C3%BCrken", then the client will receive "t%C3%BCrken", but your iOS client applications can easily remove the percent encoding by using removingPercentEncoding.
I managed to get a node to communicate to an android app. The message goes from the node to the gateway. Using node-red the gateway publishes the message to an mqtt broker and I subscribe to the right topic to receive that message on my android app.
Only problem I have now is that I never realized that the msg was encoded and I'm quite lost now. I receive an array of bytes on the app side and I'm not sure what is the next step I have to take to be able to read that message.
Thanks for the help!
You are actually receiving bytes from Node-Red. You could either convert the received bytes into String on your Android or modify your lora input node's data type from Bytes to UTF-8.
I have a function to decode it:
var x = Buffer.from(msg.payload.raw,"base64").toString()
msg.Decrypted = parseFloat(x)
return msg;
In this Instructable i explained how to decode Lora messages in node red
https://www.instructables.com/id/Lora-Temperature-Dashboard/
On a MultiTech Conduit, the message is base64 encoded by the internals of the MTC, then published to the internal mqtt broker at lora//up, so you need to base64 decode it, then do whatever you need to with it. You can use the built in mosquitto applications to subscribe to this topic, or any other mqtt client (paho libraries, etc)
I 'm developing an android app. Integrating PubNub with our app for Push Notifications.
I'm getting error while Publishing an message to PubNub using PubNub Api method called Publish.
I'm getting error especially while publishing the below two special characters.
"
\
Encoding/Decoding is correct, but also, be sure you do not send JSON content as a string. Always send a JSON object if the content is JSON data.
I am trying GCM based android app to push messages from server to android client. I am able to push fix string with the following coe. I am wondering about the ways to push XML file from server and parse at the android application. I have done some research but I couldn't find push XML rather I found send XML file. Thank you
if (androidArray.size() == 1) {
String registrationId = androidArray.get(0);
Message message = new Message.Builder()
.collapseKey(collapseKey)
.timeToLive(30)
.delayWhileIdle(true)
.addData("message", Message)
.build();
Result result = sender.send(message, registrationId, 5);
You don't push xml (or JSON preferably) to the android app. You send a simple message to the app.
when the app receives the message it then needs to go and pull the xml/json from the website with an http get request to the relevant url that will supply the xml.
The android app can then parse the response and do whatever you want it to.
Here is an EXCELLENT tutorial on C2DM (The forerunner to GCM) http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html
You should be able to work out the differences needed.
UPDATE
Google Android has a complete section on GCM which can be found here
http://developer.android.com/google/gcm/index.html
Within that link there are getting started guides and a GCM Demo app
There are limits to the amount of data you can send and you should not rely on your data not ever exceeding the limits or Google arbitrarily changing the amount of data you are allowed to send.
Should either of those occur you would need to update your app so just do it right in the first place.
The message you send should act as a "key" to determine what action to take when the message is received.
UPDATE
If you are feeling REALLY adventurous you could use a custom sync adapter to help you consume your web services. It's pretty advanced stuff but if you are feeling curious about this then watch the Google I/O seminar on consuming RESTfull web services http://www.youtube.com/watch?v=xHXn3Kg2IQE
I have a single android application, which supports for 7 countries(Localization and Internationalization). The application functionality and language changed based on the device locale.
I need to implement the GCM push notifications for this application.
Requirement:
Is it possible to send the push notification in 7 different languages with single GCM account.
Is there any way to display the push notification in their device local language.
You can either take the approach suggested by Ascorbin, or implement something similar to what Apple have in their push notifications:
Your server can send a GCM message with a parameter that is a key to a message. Yout Android App will have to contain for each possible key the strings that should be displayed for it in each of the 7 languages (using multiple copies of strings.xml). Then the GCM reciever in your app will get the key from the server and get the resource string that matches it (it will automatically get the string that matched the locale of the device). This way you don't have to worry about localization in your server. The downside of this approach is that all your messages have to be predefined in your app.
You can also add parameters to the message key like Apple do.
For example, the server sends a key = "NEW_MAIL_FROM" and param1 = "John". The app finds a string resource for that key (lets assume the device used English locale) - "You have a message from {0}" - and replaces the param with John, displaying the message "You have a message from John". A device with a differennt locale will show a message in a different language.
You can implement that server-side, after GCM registration with the send of token, send also the device locale. And then notify users instantly with a localized message.
Payload is something "sort" its not a good idea to pass through it so much information.
On the other hand if you have fixed messages you can use:
private void handleMessage(Intent intent) {
// server sent key-value pairs
String name_of_resource = intent.getExtra("message_id");
int id = getResources().getIdentifier(name_of_resource, "string", getPackageName());
if (id != 0) {
String text = getString(id); // the text to display
// generates a system notification to display here
}
}
see http://developer.android.com/google/gcm/gcm.html#received_data for handling received data.
When the devices register at your server, let them send the Locale. So you can have locale groups of devices and send the messages in according languages.
You can easily localize your GCM notification using title_loc_key and body_loc_key. These keys listed in official GCM docs.
More details can be found here.
Send a GCM Push from server (without any language specific data).
In response to the push, the client makes a REST api call to the server with it's language as a Query parameter.
The server fetches appropriate language's text and send back to the client on real time.