0

I'm interested in how apps like WhatsApp implement this feature. I've just read that it's bad practice to send the message directly from device to device, because someone can eventually get to the API key and send messages as my application.

What's the solution then? Having a server and making the devices only communicate with this server, so when I want to send a message to another device, it goes to the server and from server to that target device?

Also another question:

 public void onMessageReceived(String from, Bundle data)

This is a method of the GcmListenerService. When is "from" different than the default senderID? Because I tried sending the message directly from one device to another and the "from" was still the default senderID that I got when I registered the app on the website.

Greyshack
  • 1,901
  • 7
  • 29
  • 48
  • check answer over here, http://stackoverflow.com/a/16143572/1168654 – Dhaval Parmar Jul 27 '15 at 12:05
  • it doesn't really answer my question, I am interested in device to device notifications(messages) and just the workflow – Greyshack Jul 27 '15 at 12:10
  • have you tried GCM demo?? over here: https://developers.google.com/cloud-messaging/android/start they tell us GCM send notification to all device, or particular device if you have device id, so we can say that "to send notification we need GCM server?? yes or no". in simple way to send notification device to device not possible but you can call GCM server from device, which is already described here http://stackoverflow.com/a/16143572/1168654 – Dhaval Parmar Jul 27 '15 at 12:17
  • I have tried it. And I'm not sure you get my question. I know the notification is received only from the GCM server and cannot be received from another device. But my question is what is the best way to implement messaging based on GCM service. Because one way to do that is to send a message to my own server application, which then sends it to the GCM server, which is then sent to the target device – Greyshack Jul 27 '15 at 12:21
  • you are going on right path, but you have to do this with socket programming to call server or you can check this service https://github.com/redsolution/xabber-android – Dhaval Parmar Jul 27 '15 at 12:26
  • I have some experience programming socket servers. So you're telling me it's a good solution? To communicate with server via sockets and then the server sends the notifications to target devices? – Greyshack Jul 27 '15 at 12:30
  • read this, http://stackoverflow.com/a/25743542/1168654 – Dhaval Parmar Jul 27 '15 at 12:44
  • yep I've decided that RESTful server will be better too :) ok thanks for help – Greyshack Jul 27 '15 at 12:55

1 Answers1

0

Basically, you need a server to listen to upstream messages and broadcast then again to intended recipients.

You can find an article describing this here: http://javapapers.com/android/android-chat-with-google-gcm-xmpp/

The approach is pretty limited and not as flexible as having your own messaging server, but should be enough for a proof of concept.

Mickaël Rémond
  • 9,035
  • 1
  • 24
  • 44