1

我创建了一个应用程序,它使用 GCM 从我们的服务器接收通知,正在使用 Google 的 gcm.jar,并有一个 customGCMIntentService 类处理它。我的问题是只有一部手机在呼叫时实际上从 GCM 获得响应(或更可能正确路由响应)GCMRegistrar.register()

我所看到的是 GCMRegistrar(来自 gcm 库)正确地将我的自定义广播接收器设置为 nexus 3 手机上的重试接收器,这是唯一一个有效的,但在其他手机上无效。这让我相信我可能会收到来自 GCM 的回复,但它没有在其他三部手机上得到处理(详情如下)。

GCMRegistrar Setting the name of retry receiver class to <My_application_package>.CustomGCMBroadcastReceiver

他们都有一个允许与 GCM 通信的数据连接,他们都有一个活动的 gmail 帐户(他们都有 playstore 工作)。我还毫无问题地在它们上运行了 Google 的 GCM 演示。

注册码

 //This is almost identical to how Google's GCM demo does it.
 private void registerOnGCM(){
    checkNotNull(SERVER_URL, "SERVER_URL"); //What it says on the tin
    checkNotNull(SENDER_ID, "SENDER_ID");
    // Make sure the device has the proper dependencies.
    GCMRegistrar.checkDevice(this);
    // Make sure the manifest was properly set - comment out this line
    // while developing the app, then uncomment it when it's ready.
    GCMRegistrar.checkManifest(this);
    /*
    registerReceiver(mHandleMessageReceiver,
            new IntentFilter(HANDLE_MESSAGE));
    */
    final String regId = GCMRegistrar.getRegistrationId(getApplicationContext());
    if (regId.equals("")) {
        // Automatically registers application on startup.
        GCMRegistrar.register(getApplicationContext(), SENDER_ID);
    } 
    else {
        // Device is already registered on GCM, check server.
        if (GCMRegistrar.isRegisteredOnServer(getApplicationContext())) {
            // Skips registration.
            Log.i(TAG, "Already registered");
        } else {
            // Try to register again, but not in the UI thread.
            // It's also necessary to cancel the thread onDestroy(),
            // hence the use of AsyncTask instead of a raw thread.
            final Context context = this;
            mRegisterTask = new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                boolean registered = ServerUtilities.register(context, regId);

                if (!registered) {
                    GCMRegistrar.unregister(context);
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                mRegisterTask = null;
            }

        };
        mRegisterTask.execute(null, null, null);
    }
}

CustomGCMBroadcastReceiver 的代码无关紧要,GCMIntentService 也无关紧要,因为它们都没有被调用过。问题不在于他们。

我正在测试的设备

  • HTC Desire GSM 运行 CyanogenMod 7
  • 运行 Android 4.1.1 的 Google Nexus 3(应用程序适用于这个)
  • 运行 Android 4.0.1 的摩托罗拉 T910
  • 运行 Android 4.2.6 的三星 Galaxy mini

清单

<uses-sdk  android:minSdkVersion="8" android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- App has permission to read/write files on sd card. Used for RSS document -->
<uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>

<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<permission
    android:name="<My_package_name>.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission
    android:name="<My_package_name>.permission.C2D_MESSAGE" />

<!-- This app has permission to register and receive data message. -->
<uses-permission
    android:name="com.google.android.c2dm.permission.RECEIVE" />



<application
    android:allowBackup="true"
    android:theme="@style/AppTheme"
    android:label="@string/app_name">
    <activity
        android:name="<My_package_name>.MasterActivity"
        android:screenOrientation="portrait">
    </activity>

    <activity   android:name="<My_package_name>.WebActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!--
      BroadcastReceiver that will receive intents from GCM
      services and handle them to the custom IntentService.

      The com.google.android.c2dm.permission.SEND permission is necessary
      so only GCM services can send data messages for the app.
    -->
    <receiver
        android:name="<My_package_name>.CustomGCMBroadcastReceiver"
        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="<My_package_name>" />
        </intent-filter>
    </receiver>

    <!--
      Application-specific subclass of GCMBaseIntentService that will
      handle received messages.

      By default, it must be named .GCMIntentService, unless the
      application uses a custom BroadcastReceiver that redefines its name.
    -->
    <service    android:name="<My_package_name>.GCMIntentService"
                android:enabled="true"/>
 </application>

可能是我忘记添加的东西。让我知道。

欢迎所有想法。

-德累斯顿先生

4

1 回答 1

1

好吧,它似乎已经通过清理、重建然后将其部署到其他设备来自行修复。

为什么从所有手机上删除所有痕迹,然后在调试模式下运行它们不起作用,除了在一部手机上我不知道。

于 2013-02-14T17:30:28.357 回答