让我试着解释一下基本的工作原理
你需要-一台服务器和两部手机
第 1 步您有一个服务器,两部手机都连接到该服务器。
第 2 步您可以通过 link 访问 Google Developers 页面
第 3 步您在那里创建一个新项目,然后将 GCM API 添加到您的项目中。保存项目的 sender_id。还要检查 API 的凭据并添加新的 浏览器密钥 并保存该密钥。
第 4 步 将 GCM 添加到您的项目中。为此,请转到安装 Eclipse 的根目录。然后导航到文件夹extras/google/google-play-services/libsproject/,然后将那里的文件夹复制到另一个位置。然后将该文件夹导入 Eclipse 中的工作区并更改其属性以使其成为库。然后将其添加到您的应用程序项目中。
第 5 步您需要在 GCM 中注册您的手机。为此,您可以使用这样的示例代码
// to start process
public void registerDevice() {
// TODO Auto-generated method stub
try {
if (checkPlayServices()) {
regid = getRegistrationId();
if (regid.isEmpty()) { // creating a new reg id
registerInBackground();
} else
saveid();
} else{
}
} catch (Exception e) {
}
}
// to create a new id
private void registerInBackground() {
// TODO Auto-generated method stub
try {
AsyncRegister logMeIn = new AsyncRegister("<you project id here>", context);
logMeIn.doneWith = this;
logMeIn.execute();
System.out.println("execute complete");
} catch (Exception e) {
}
}
// to fetch stored registration id
private String getRegistrationId() {
// TODO Auto-generated method stub
try {
int i=myPrefs.getInt("cuid", 0);
if(id!=i){
myPrefs.edit().putInt("cuid", id).commit();
int registeredVersion = myPrefs.getInt("appversion",
Integer.MIN_VALUE); // to check if app is updated
int currentVersion = getAppVersion();
if (registeredVersion != currentVersion) {
myPrefs.edit().putInt("appversion",currentVersion).commit();
}
return "";
}
String registrationId = myPrefs.getString("regid", "");
if (registrationId.isEmpty()) {
return "";
}
int registeredVersion = myPrefs.getInt("appversion",
Integer.MIN_VALUE); // to check if app is updated
int currentVersion = getAppVersion();
if (registeredVersion != currentVersion) {
myPrefs.edit().putInt("appversion", currentVersion).commit();
return "";
}
// when id is stored previously then this is called
return registrationId;
} catch (Exception e) {
}
}
//to fetch the current app version
private int getAppVersion() throws NameNotFoundException {
// TODO Auto-generated method stub
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
return packageInfo.versionCode;
}
// to check if play services are there on the device
private boolean checkPlayServices() {
// TODO Auto-generated method stub
try {
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(context);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
toSend = new Bundle();
//tell user google play needs to be updated
} else {
//tell user their system does not support GCM
}
myPrefs.edit().putBoolean("support", false).commit();
return false;
}
myPrefs.edit().putBoolean("support", true).commit();
return true;
} catch (Exception e) {
}
}
第 6 步 保存注册 ID 并将其传递到您的服务器进行保存。
第 7 步 当用户单击发送按钮时,将消息传递到您的服务器。服务器将提取与 DB 一起存储的 id 列表,然后将消息与这些 id 一起推送到 GCM。
第 8 步 现在 GCM 会将该消息发送到所有为其提供 id 的手机。
第 9 步在您的应用中添加一个接收器,用于接收来自 GCM 的文本,然后将其显示给用户。
这是一个简短的解释。如果您想详细了解这一点,请阅读在线教程。否则,您也可以在我的群组中给我发短信(您可以在我的个人资料描述中找到链接),我很乐意为您提供帮助。