and*_*nia 6 android google-cloud-messaging
我今天了解了GCM服务器.阅读developer.android.com部分GCM中的内容,我仍然不确定如何管理第三方服务器中的注册ID.我的理解是:
那么,我如何从GCM服务器获取设备注册ID?我需要直接从Android设备?
感谢您的意见.
了解基本信息的最佳地点是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);
...
}
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,您对如何将 GCM RegistrationId 获取到服务器实现感到困惑。您可以在sendRegistrationIdToBackend()您定义的方法中执行此操作。
您需要创建一个端点来接收 POST 请求,每个设备在收到来自 GCM 的注册 ID 后都会将其 RegistrationID 发布到该请求。然后,您的服务器实现将了解所有安装了您的应用程序并已向 GCM 注册的设备。
注意:您可能需要注意的一件事是,如果设备最终发布其 RegistrationID 两次,或者用户卸载然后重新安装应用程序,但 GCM 为他们提供了相同的 RegistrationID,您最终可能会在其中存储重复的 RegistrationID,则不要创建重复的 RegistrationID您在服务器上的数据库。如果发生这种情况,并且您想要向所有用户发送推送,那么您的某些用户可能最终会收到多个推送通知,具体取决于数据库中有多少重复项。
| 归档时间: |
|
| 查看次数: |
3472 次 |
| 最近记录: |