18

この問題について他の回答を試してみましたが、解決できません。送信者 ID が正しく、権限が追加され、API キーが true です。

プロジェクトの作成には、この投稿を使用します: http://developer.android.com/guide/google/gcm/gs.html#server-app

    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
      GCMRegistrar.register(this, SENDER_ID);
      regId = GCMRegistrar.getRegistrationId(this);
    } else {
      Log.v(TAG, "Already registered");
    }
    String did = getDeviceID();

空の文字列を返します。

4

6 に答える 6

49

アプリのルートパッケージで正しくGCMIntentService定義されていますか?

<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >

  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
    <category android:name="my_app_package" />
  </intent-filter>
</receiver>

これがアプリのメインパッケージと同じであることを確認してください。"my_app_package"同じでない場合、IDは空の文字列を返します。

また、注意してください

    GCMRegistrar.register(this, "...");

非同期です。GCMRegistrar.getRegistrationId(this)したがって、その直後に呼び出すことは信頼できないので、避ける必要があります。

IDはGCMIntentService、登録手順の一部として、ブロードキャストコールバックを介してに到着します。そこから、gcm / fcmキーを好きな場所に保存して、アプリケーションの他の部分(通常はサーバー上)で使用できます。

于 2012-07-31T08:34:11.483 に答える
11

アプリケーションを初めて起動する場合は、GCMIntentService.javaファイルでOnRegisteredコールバックを待つ必要があります。

GCMRegistrar.register(this, SENDER_ID);
// Android does NOT wait for registration ends and executes immediately line below
// So your regId will be empty.
regId = GCMRegistrar.getRegistrationId(this);

GCMIntentService.java:

@Override
protected void onRegistered(Context c, String regId) {
    // You can get it here!
}

編集:新しいバージョンのGCMライブラリ(Google Playサービスライブラリにバンドルされています)は応答を待ち、登録IDを返します。したがって、この答えは古いGCMライブラリ用です。

于 2013-02-15T12:06:17.093 に答える
5

私は解決策を見つける1日の闘争の後に同じ問題を抱えています。まず、GCM 関連のコードをメイン パッケージに入れ、androidManifest.xml のマイ パッケージに従って変更を加えます。

簡単な例を挙げます。私のプロジェクト名は GCMExample で、3 つのパッケージがあります。

GCM 関連のクラス ファイルを 2 番目のパッケージに入れると、登録 ID が取得されません

1. GCMIntentService 、2. ServerUtilities 、3. CommonUtilities 4. WakeLocker のような私の GCM 関連クラス com.example.GCMExample

私のandroidManifest.xmlも

     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />
     <uses-permission android:name="android.permission.WAKE_LOCK" />
     <uses-permission android:name="android.permission.VIBRATE" />

     <permission android:name="com.example.GCMExample.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

     <uses-permission android:name="com.example.GCMExample.C2D_MESSAGE" />

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
        <!-- Receives the actual messages. -->
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <!-- Receives the registration id. -->
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

        <category android:name="com.example.GCMExample" />
      </intent-filter>
</receiver>  

<service android:name="com.example.GCMExample.GCMIntentService" />
于 2014-02-20T05:48:33.360 に答える
1

registration.one から応答が得られない場合、主な理由の 1 つは、マニフェスト ファイルが正しく構成されていないことです...特に、「package_name」(com.xxx.yyy などのアプリ パッケージ名) を正しく指定します。

于 2013-06-28T22:49:45.607 に答える