我試圖用GCM註冊我的android應用程序,我按照this link中的說明獲取api密鑰,我做了1-7中編寫的內容,但是在步驟8中我可以找不到 Android Key部分。獲取GCM的API密鑰
我該怎麼辦?
p.s. 當我嘗試使用gcm gcm.register(SENDER_ID)
註冊我的設備時,出現錯誤,提示服務不可用,是否存在連接問題?或者這個錯誤是別的? (我沒有寫應用程序的服務器部分)
Android清單:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appspot.smartgan"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.appspot.smartgan.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.appspot.smartgan.permission.C2D_MESSAGE" />
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light" >
<activity
android:name="com.appspot.smartgan.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.appspot.smartgan" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.appspot.smartgan.LoginActivity"
android:label="@string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateVisible" >
</activity>
<activity
android:name="com.appspot.smartgan.ChildActivity"
android:label="@string/title_activity_child" >
</activity>
</application>
</manifest>
註冊碼:(我使用的代碼從一個例子谷歌公佈)
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
String str = "";
@Override
protected String doInBackground(Void... params) {
String message = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
message = "Device registered, registration ID=" + regid;
Log.d("SMARTGAN_PLAY", "register completed");
// send registration id to the server
sendRegistrationIdToServer();
// Persist the regID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException e) {
message = "Error:" + e.getMessage();
}
str = message;
return message;
}
@Override
protected void onPostExecute(String message) {
childrenTextView.setText(str);
}
}.execute(null, null, null);
}
我已經添加了清單和註冊碼 –
我也有同樣的問題。我注意到@meda有實際的整數作爲項目ID,但在我這邊項目被識別爲字符串「prismatic-petal-438」,並且我得到了invalid_sender消息。有很多貶值的c2dm的例子,但gcm沒有那麼多 – vodich
對不起剛發現你需要使用項目編號作爲項目ID duh ... – vodich