2013-12-17 318 views
1

我試圖用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); 
} 

回答

1

你不爲了註冊到GCM,不需要API密鑰。您只需要一個發件人ID(這是您爲應用程序創建的Google API項目的項目ID)。 API密鑰僅在服務器端用於將GCM消息發送到您的應用程序。

獲取服務不可用的原因可能很多。您必須發佈您的清單和客戶端註冊代碼才能獲得更多幫助。

+0

我已經添加了清單和註冊碼 –

+0

我也有同樣的問題。我注意到@meda有實際的整數作爲項目ID,但在我這邊項目被識別爲字符串「prismatic-petal-438」,並且我得到了invalid_sender消息。有很多貶值的c2dm的例子,但gcm沒有那麼多 – vodich

+0

對不起剛發現你需要使用項目編號作爲項目ID duh ... – vodich

0
//Subtitute with your own project ID 
String SENDER_ID = "46086692976"; 

見截圖: enter image description here

+0

我不明白你想說什麼。 SENDER_ID被定義並具有正確的價值,我只是不認爲我很重要。 –

+0

以及錯誤表明SENDERID是無效的,你需要確保它正確的一個我已經beentgrough該bbefore – meda

+0

我不知道該怎麼辦...它從雲端控制檯複製項目編號(CTRL + C) –