在服务器中管理 GCM 注册 ID
Manage GCM Registration ID in Server
我今天了解了 GCM 服务器。阅读 developer.android.com 部分 GCM 中的内容,我仍然不确定如何在第三方服务器中管理注册 ID。我的理解是:
- android 应用程序可以注册和注销其 ID from/to GCM 服务器。
- 服务器端可以使用注册 ID 和
将消息发送到设备
服务器密钥。
那么,如何从 GCM 服务器获取设备注册 ID?我需要直接从 android 台设备获取吗?
感谢您的建议。
您需要按照以下步骤从 android 设备获取 gcm Id。每个设备的每个应用程序都会有一个唯一的 gcm Id。
- 您必须在 Google api console
上注册
- 按照此 link 从应用程序获取 gcm id
了解基本信息的最佳位置是 here developer.android.com
您将在他们的教程中看到此示例代码:
/**
* Registers the application with GCM servers asynchronously.
* <p>
* Stores the registration ID and app versionCode in the application's
* shared preferences.
*/
private void registerInBackground() {
new AsyncTask() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;
// You should send the registration ID to your server over HTTP,
// so it can use GCM/HTTP or CCS to send messages to your app.
// The request to your server should be authenticated if your app
// is using accounts.
sendRegistrationIdToBackend();
// For this demo: we don't need to send it because the device
// will send upstream messages to a server that echo back the
// message using the 'from' address in the message.
// Persist the registration ID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
// If there is an error, don't just keep trying to register.
// Require the user to click a button again, or perform
// exponential back-off.
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
mDisplay.append(msg + "\n");
}
}.execute(null, null, null);
...
}
如果我理解正确的话,您对如何将 GCM RegistrationId 获取到您的服务器实现感到困惑。您可以在您将定义的 sendRegistrationIdToBackend()
方法中执行此操作。
您需要创建一个端点来接收 POST 请求,每个设备在收到来自 GCM 的请求后将 post 其注册 ID。然后您的服务器实现将知道所有安装了您的应用程序并已向 GCM 注册的设备。
注意:您可能需要注意的一件事是,如果设备最终两次 post 注册他们的注册 ID,或者用户卸载然后重新安装应用程序但 GCM 给他们相同的 RegistrationID,您最终可能会在服务器的数据库中存储重复项。如果发生这种情况并且您想向所有用户发送推送,那么您的一些用户可能最终会收到多个推送通知,具体取决于数据库中有多少重复项。
首先,您不会从 GCM 服务器获得任何注册号。您只会从 Google Developer Console. You have to create registration ID using your client app. You have to write some codes to create registration ID. You can follow this link 获得 Project Id
和 App Key
。这里解释的很好
我今天了解了 GCM 服务器。阅读 developer.android.com 部分 GCM 中的内容,我仍然不确定如何在第三方服务器中管理注册 ID。我的理解是:
- android 应用程序可以注册和注销其 ID from/to GCM 服务器。
- 服务器端可以使用注册 ID 和
将消息发送到设备 服务器密钥。
那么,如何从 GCM 服务器获取设备注册 ID?我需要直接从 android 台设备获取吗?
感谢您的建议。
您需要按照以下步骤从 android 设备获取 gcm Id。每个设备的每个应用程序都会有一个唯一的 gcm Id。
- 您必须在 Google api console 上注册
- 按照此 link 从应用程序获取 gcm id
了解基本信息的最佳位置是 here developer.android.com
您将在他们的教程中看到此示例代码:
/**
* Registers the application with GCM servers asynchronously.
* <p>
* Stores the registration ID and app versionCode in the application's
* shared preferences.
*/
private void registerInBackground() {
new AsyncTask() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;
// You should send the registration ID to your server over HTTP,
// so it can use GCM/HTTP or CCS to send messages to your app.
// The request to your server should be authenticated if your app
// is using accounts.
sendRegistrationIdToBackend();
// For this demo: we don't need to send it because the device
// will send upstream messages to a server that echo back the
// message using the 'from' address in the message.
// Persist the registration ID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
// If there is an error, don't just keep trying to register.
// Require the user to click a button again, or perform
// exponential back-off.
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
mDisplay.append(msg + "\n");
}
}.execute(null, null, null);
...
}
如果我理解正确的话,您对如何将 GCM RegistrationId 获取到您的服务器实现感到困惑。您可以在您将定义的 sendRegistrationIdToBackend()
方法中执行此操作。
您需要创建一个端点来接收 POST 请求,每个设备在收到来自 GCM 的请求后将 post 其注册 ID。然后您的服务器实现将知道所有安装了您的应用程序并已向 GCM 注册的设备。
注意:您可能需要注意的一件事是,如果设备最终两次 post 注册他们的注册 ID,或者用户卸载然后重新安装应用程序但 GCM 给他们相同的 RegistrationID,您最终可能会在服务器的数据库中存储重复项。如果发生这种情况并且您想向所有用户发送推送,那么您的一些用户可能最终会收到多个推送通知,具体取决于数据库中有多少重复项。
首先,您不会从 GCM 服务器获得任何注册号。您只会从 Google Developer Console. You have to create registration ID using your client app. You have to write some codes to create registration ID. You can follow this link 获得 Project Id
和 App Key
。这里解释的很好