我们必须创建一个带有推送通知的GCM服务的android项目。 当我们尝试使用GoogleCloudMessaging.register(...)函数注册GCM时,它会失败并抛出AUTHENTICATION_FAILED异常。我们不知道为什么。
以下是我们使用的google初始化代码:
private void initGoogleCloudMessaging() {
// Check device for Play Services APK. If check succeeds, proceed with
// GCM registration.
if (checkPlayServices()) {
gcm = GoogleCloudMessaging.getInstance(this);
String regId = getGoogleCloudMessagingRegistrationID(context);
if (regId.isEmpty()) {
registerGoogleCloudMessagingInBackground();
}
} else {
Log.e(GCM_TAG, "No valid Google Play Services APK found.");
}
}
private void registerGoogleCloudMessagingInBackground() {
Log.i("GCM", "Registering..");
new AsyncTask() {
@Override
protected String doInBackground(Object... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
String regId = gcm.register(Configuration.GoogleCloudMessagingSenderID);
msg = ">>Device registered, registration ID=" + regId;
storeGoogleCloudMessagingDeviceID(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.
Log.e("GCMTest", "GCM: Registration exception: " + ex.getMessage());
//oldSchoolRegister();
}
return msg;
}
}.execute(null, null, null);
}
我们使用正确的项目编号。
以下是我们的XML文件的google部分:
<!-- Permissions necessary for Google's Cloud Messaging Service -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
我希望你可以帮助我,因为它非常令人沮丧。