我已成功向本地管理的设备组注册我的 Android 应用程序,如下所述:Google Cloud Messaging (GCM) with local device groups on Android gives HTTP Error code 401 .
这工作正常,我可以按照此处的指南将 GCM 消息从 Android 发送到 Android:https://developers.google.com/cloud-messaging/downstream .
但是,这使用了 SERVER_API key ,据说将其放在客户端上并不是一件好事。
我的问题是:在客户端使用 SERVER_API key 是否有问题?
其次:是否可以在不使用 SERVER_API key 的情况下发送 GCM 消息?
我尝试将从设备组注册收到的 notification_key 传递给此方法,但没有任何结果:
private void sendMessage2(String recipient) throws IOException {
Log.i(TAG, "Sending message to " + recipient);
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
AtomicInteger msgId = new AtomicInteger();
String id = Integer.toString(msgId.incrementAndGet());
Bundle data = new Bundle();
data.putString("hello", "world");
gcm.send(recipient, id, data);
Log.i(TAG, "Successfully sent message to " + recipient);
}
// recipient is the notification_key of the device group.
最佳答案
不要在您的客户端上使用 SERVER_API key !
此 key 是 secret 的,不会在您的二进制文件中进行混淆处理。有人可以轻松下载您的 APK,运行 strings
(或类似工具),然后代表您的应用程序开始发送 GCM 消息。
如果你想做 Android <--> Android 消息传递,你实际上需要做 Android <--> Server <--> Android。
关于android - 在客户端上使用 SERVER API key 是否可以,是否可以在不使用 SERVER API key 的情况下向设备组发送 GCM 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34322968/