Can gcm be used to send and receive messages between android devices?

≯℡__Kan透↙ 提交于 2019-12-07 18:06:51

问题


I have two android devices that runs a gcm client app. I get the Reg IDs of both the devices. I also have a server API key. Now the thing is, Using a simple php code using cUrl or Zend framework, I'm able to send message from a web server to one of my droids (Of course I can broadcast too). But, with this in hand, is there any way to send a string from one device to another?


回答1:


Quoting myself from my book:

You might be tempted to use GCM for peer-to-peer messaging, without a server of your own. In effect, each Android app is its own server, using the same JAR you might use in a Web app inside your Android app to send messages to some other party. For example, you could implement a chat system without having a dedicated chat server.

The danger here is that this would require your API key to be embedded within your Android application. Anyone with that API key is perfectly capable of forging messages from you. The IP address restrictions you could place on that API key are unlikely to help, since your legitimate uses might come from any IP address, not just some single server. Since finding magic strings in APK files is not that difficult for those with the inclination, putting your API key in your APK file is a dangerous move.

Hence, you will want some server of your own as a middleman.




回答2:


One thing you could do is to make a POST request to your web server with the message and the registration ID of the other Android device, but that would be a little cumbersome as you'd have to figure out a way to retrieve the registration IDs of the other Android devices from your web server.

I would recommend looking into XMPP as this protocol has been built from the ground up to be an extensive messaging protocol. I'm pretty sure you'll find good XMPP server frameworks to implement your functionality.



来源:https://stackoverflow.com/questions/12565301/can-gcm-be-used-to-send-and-receive-messages-between-android-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!